问题
I've been working with HTML strings obtained from an XML file. I'm trying to figure out a way to display these strings in a richtextbox with formatting. So e.g.
<p>This is a <strong>HTML</strong> string from the <em>XML</em> file</p>
or
<p>This is our <span style="text-decoration: underline;">response</span></p>
Should be displayed in the richtextbox like so:
This is a HTML string from the XML file
This is our response <<-- this should be underlined
I'm not too sure how to go about with this. And I'm not too sure as to how the WebBrowser class would work here as the HTML strings are individual, and won't form a complete HTML file.
Additionally, I need a way to reverse the formatting (HTML encode) once any changes are made in the richtextbox as they'll be written back to the XML file.
Is there a way for me to achieve this? I really could use the help. I'm stumped.
EDIT:
If it helps, here's the part of the XML where I get my strings from (something similar to this).
<Field id="13598" type="1"><p><strong>This </strong>is our <em>response</em></p></Field>
I'm basically trying to allow users to edit and save the string in Field.
EDIT 2:
I've managed to use a WebBrowser control to sort of fulfill my requirements, but I'm facing a new problem. Have a look here: Editing HTML with a WYSIWYG Editor Any help would be greatly appreciated!
回答1:
I've solved it using a WYSIWYG HTML editor. However, I'm facing a new problem, if anyone could help, that'd be great!
Here's my new question: Editing HTML with a WYSIWYG Editor
回答2:
Are you using an extended RTB to display html because as far as i know from CodeProject site, .NET RichTextBox control only allows you to save and load files using RTF codes or plain text files. So you need to extend the RTB to get that function.
Here's the link - RTB to save Html.
But if you can avoid it and load xml directly to RTB, its much better as working code is here.
And for more info on html formatting code to RTB, check here
回答3:
Winz, I'm a bit lost as to what your requirements are.
Based on your comment...
"I'm trying to display them in a RTB as how they'd appear in on a Web Page"
You want to render the HTML markup as an actual page, for example an
<img />
tag renders as an actual image.You want to render the HTML markup as text as it would appear if you were to "View Page Source" using various syntax colors
The problem...
HTML markup and the RTF specification are vastly different, and therefore you would need to convert the markup to RTF to make a RichTextBox display HTML as a page would appear!
To solve your problems...
To render HTML markup in .NET as a web page you can use the built in WebBrowser control. This control uses Microsoft's IE engine in the background, but be warned, it is not very friendly to certain standards such as HTML5 and CSS3. You can improve how it renders the page using (IIRC) browser feature controls
To render the HTML markup as text, the way it would appear if you were to "View Page Source", using syntax coloring, you can use a RichTextBox to an extent. Here is an example using XML, however since XML and HTML are so similar, you can probably modify the solution for HTML syntax rendering.
Note: Syntax highlighting is basically just rendering text to use certain colors for certain things, I.E. a tag might be in red, whilst an attribute might be in blue, and a comment might be green.
Here is another syntax highlighter example:
http://dotnet-csharp-programming.blogspot.co.uk/2010/04/xml-highlighting-in-rich-textbox.html
Other solutions...
For rendering HTML content as a page, you might consider finding a 3rd party browser control for .NET. I know there is one for the FireFox rendering engine, and there may well be one for Chrome as well. Here is a stackoverflow article about replacing the built in WebBrowser control.
For rendering HTML syntax, you might also consider using Scintilla.NET or you could buy a commercial product such as ActiPro's Syntax Editor
来源:https://stackoverflow.com/questions/13305602/html-formatting-in-richtextbox