从WebBrowser到有效的XHTML的c# HTML

时间:2020-12-16 22:30:24

So, we are using a webBrowser control in edit mode, to allow people to enter text, and then take that text and send it out to the server for everyone to see. IE, it's an HTML input box.

因此,我们在编辑模式中使用webBrowser控件,允许人们输入文本,然后将文本发送到服务器,让每个人都能看到。它是一个HTML输入框。

The HTML output from that box isn't standard XHTML, given that it's just a webBrowser control, so i needed a method to convert any bad HTML to XHTML. I read up on SGML, and subsequently have used:

这个框的HTML输出不是标准的XHTML,因为它只是一个webBrowser控件,所以我需要一个方法来将任何坏的HTML转换成XHTML。我阅读了SGML,并随后使用:

private static string Html2Xml(string txtHtmlString)
    {
        var xhtml = new Sgml.SgmlReader();
        var sw = new StringWriter();
        var w = new XmlTextWriter(sw);
        xhtml.DocType = "HTML";
        xhtml.InputStream = new StringReader(txtHtmlString);

        while ((!xhtml.EOF))
        {
            w.WriteNode(xhtml, true);
        }

        w.Close();
        return sw.ToString();
    }

I basically pase HTML string to that method, and it returns 'suposed' proper XHTML. However, it's not passing XHTML checks, and the data it returns is just a basic

我基本上是将HTML字符串pase到那个方法中,它返回适当的XHTML。但是,它没有传递XHTML检查,它返回的数据只是一个基本的数据

<html><head></head><body></body></html> 

Format. Thus, not proper XHTML.

格式。因此,不正确的XHTML。

So, how can i format that to actually output proper XHTML? There isn't much on MindShares site for SGML documentation anymore, so not sure where to go from here.

那么,如何将其格式化以实际输出适当的XHTML呢?在MindShares站点上已经没有太多关于SGML文档的内容了,所以不知道从这里开始要做什么。

Essentially, we need the HTML from the WebBrowser control, which isn't valid XHTML, to output to XHTML, so that we can attach it to an XMPP.msg.Html element (valid XHTML only). If the system detects that any codes within the HTML is invalid, it marks the XMPP.msg.Html as blank, so i know the above method isn't working.

本质上,我们需要WebBrowser控件中的HTML(它不是有效的XHTML)输出到XHTML,以便将其附加到XMPP.msg。Html元素(仅适用于XHTML)。如果系统检测到HTML中的任何代码无效,它将标记XMPP.msg。Html为空,所以我知道上面的方法不起作用。

Thanks!

谢谢!

1 个解决方案

#1


9  

Would reccomend using either something like TinyMCE or HtmlAgilityPack (available as a Nuget package or from codeplex).

可以使用TinyMCE或HtmlAgilityPack(可作为Nuget包或codeplex使用)之类的工具进行修复。

TinyMCE allows users to perform a rich text edit with appropriate formatting controls, and will output the resultant Html.

TinyMCE允许用户使用适当的格式控件执行富文本编辑,并将输出结果Html。

HtmlAgilityPAck on the other hand is a library that will allow you to pass in the HtmlStream generated by your method, and output this as a valid Xhtml stream.

另一方面,HtmlAgilityPAck是一个库,它允许您传入方法生成的HtmlStream,并将其作为有效的Xhtml流输出。

Rough example for working with this in the HtmlAgilityPAck as below:

在HtmlAgilityPAck中处理此问题的粗略示例如下:

var sb = new StringBuilder(); 
var stringWriter = new StringWriter(sb);

string input = "<html><body><p>This is some test test<ul><li>item 1<li>item2<</ul></body>";

var test = new HtmlAgilityPack.HtmlDocument();
test.LoadHtml(input);
test.OptionOutputAsXml = true;
test.OptionCheckSyntax = true;
test.OptionFixNestedTags = true;

test.Save(stringWriter);

Console.WriteLine(sb.ToString());

#1


9  

Would reccomend using either something like TinyMCE or HtmlAgilityPack (available as a Nuget package or from codeplex).

可以使用TinyMCE或HtmlAgilityPack(可作为Nuget包或codeplex使用)之类的工具进行修复。

TinyMCE allows users to perform a rich text edit with appropriate formatting controls, and will output the resultant Html.

TinyMCE允许用户使用适当的格式控件执行富文本编辑,并将输出结果Html。

HtmlAgilityPAck on the other hand is a library that will allow you to pass in the HtmlStream generated by your method, and output this as a valid Xhtml stream.

另一方面,HtmlAgilityPAck是一个库,它允许您传入方法生成的HtmlStream,并将其作为有效的Xhtml流输出。

Rough example for working with this in the HtmlAgilityPAck as below:

在HtmlAgilityPAck中处理此问题的粗略示例如下:

var sb = new StringBuilder(); 
var stringWriter = new StringWriter(sb);

string input = "<html><body><p>This is some test test<ul><li>item 1<li>item2<</ul></body>";

var test = new HtmlAgilityPack.HtmlDocument();
test.LoadHtml(input);
test.OptionOutputAsXml = true;
test.OptionCheckSyntax = true;
test.OptionFixNestedTags = true;

test.Save(stringWriter);

Console.WriteLine(sb.ToString());