C# RichTextBox 中的 HTML 格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13305602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTML Formatting in RichTextBox
提问by Winz
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.
我一直在处理从 XML 文件中获取的 HTML 字符串。我正在尝试找出一种在带有格式的富文本框中显示这些字符串的方法。所以例如
<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 HTMLstring from the XMLfile
这是来自XML文件的HTML字符串
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.
我不太确定如何处理这个问题。而且我不太确定 WebBrowser 类将如何在这里工作,因为 HTML 字符串是单独的,不会形成完整的 HTML 文件。
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.
此外,一旦在富文本框中进行任何更改,我需要一种方法来反转格式(HTML 编码),因为它们将被写回 XML 文件。
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).
如果有帮助,这里是我从中获取字符串的 XML 部分(类似于此)。
<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.
我基本上是在尝试允许用户在 Field 中编辑和保存字符串。
EDIT 2:
编辑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 EditorAny help would be greatly appreciated!
我已经设法使用 WebBrowser 控件来满足我的要求,但我面临一个新问题。看看这里: 使用所见即所得编辑器编辑 HTML任何帮助将不胜感激!
采纳答案by Winz
I've solved it using a WYSIWYG HTML editor. However, I'm facing a new problem, if anyone could help, that'd be great!
我已经使用WYSIWYG HTML editor解决了它。但是,我面临一个新问题,如果有人可以提供帮助,那就太好了!
Here's my new question: Editing HTML with a WYSIWYG Editor
这是我的新问题: Editing HTML with a WYSIWYG Editor
回答by hridya pv
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.
您是否使用扩展 RTB 来显示 html,因为据我从 CodeProject 站点了解到,.NET RichTextBox 控件仅允许您使用 RTF 代码或纯文本文件保存和加载文件。因此,您需要扩展 RTB 才能获得该功能。
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.
但是,如果您可以避免它并将 xml 直接加载到 RTB,那么这里的工作代码会更好。
And for more info on html formatting code to RTB, check here
有关 html 格式代码到 RTB 的更多信息,请查看此处
回答by Matthew Layton
Winz, I'm a bit lost as to what your requirements are.
Winz,我对你的要求有点迷茫。
Based on your comment...
根据您的评论...
"I'm trying to display them in a RTB as how they'd appear in on a Web Page"
"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
您希望将 HTML 标记呈现为实际页面,例如,
<img />标签呈现为实际图像。如果您要使用各种语法颜色“查看页面源代码”,您希望将 HTML 标记呈现为文本
The problem...
问题...
HTML markup and the RTF specificationare vastly different, and therefore you would need to convert the markup to RTF to make a RichTextBox display HTML as a page would appear!
HTML 标记和RTF 规范有很大不同,因此您需要将标记转换为 RTF 以使 RichTextBox 显示 HTML 作为页面出现!
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. Hereis an example using XML, however since XML and HTML are so similar, you can probably modify the solution for HTML syntax rendering.
要将 .NET 中的 HTML 标记呈现为网页,您可以使用内置的 WebBrowser 控件。该控件在后台使用微软的 IE 引擎,但请注意,它对某些标准(如 HTML5 和 CSS3)不是很友好。您可以使用 ( IIRC)浏览器功能控件改进它呈现页面的方式
要将 HTML 标记呈现为文本,就像您“查看页面源代码”一样,使用语法着色,您可以在一定程度上使用 RichTextBox 。这是一个使用 XML 的示例,但是由于 XML 和 HTML 非常相似,您可能可以修改 HTML 语法呈现的解决方案。
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.
注意:语法高亮基本上只是渲染文本以对某些事物使用某些颜色,IE 标记可能是红色,而属性可能是蓝色,而注释可能是绿色。
Here is another syntax highlighter example:
这是另一个语法高亮示例:
http://dotnet-csharp-programming.blogspot.co.uk/2010/04/xml-highlighting-in-rich-textbox.html
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. Hereis a stackoverflow article about replacing the built in WebBrowser control.
要将 HTML 内容呈现为页面,您可能会考虑为 .NET 找到一个 3rd 方浏览器控件。我知道 FireFox 渲染引擎有一个,Chrome 也可能有一个。这是一篇关于替换内置 WebBrowser 控件的 stackoverflow 文章。
For rendering HTML syntax, you might also consider using Scintilla.NETor you could buy a commercial product such as ActiPro's Syntax Editor
对于渲染 HTML 语法,您也可以考虑使用Scintilla.NET或者您可以购买商业产品,例如ActiPro 的语法编辑器

