C#HTML从web浏览器,以有效的XHTML [英] C# HTML from WebBrowser to valid XHTML

查看:360
本文介绍了C#HTML从web浏览器,以有效的XHTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我们使用的是在编辑模式下WebBrowser控件,让人们输入文字,然后拿文本和将其发送到服务器给大家看。 IE浏览器,它是一个HTML输入框。

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.

从盒子的输出HTML不是标准的XHTML,因为它只是一个WebBrowser控件,所以我需要一种方法来什么不好的HTML转换为XHTML。我对SGML读了,随后使用了:

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:

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();
    }

我基本上PASE HTML字符串该方法,并返回suposed适当的XHTML。但是,它不是通过XHTML检查,它返回的数据仅仅是一个基本的

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><head></head><body></body></html> 

格式。因此,不妥当的XHTML。

Format. Thus, not proper XHTML.

所以,我怎么能格式化实际输出正确的XHTML?没有太多的MindShares网站SGML的文档了,所以不知道在哪里从这里走。

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.

从本质上讲,我们需要从WebBrowser控件,这不是有效的XHTML的HTML,输出到XHTML,这样我们就可以将其附加到XMPP.msg.Html元素(仅适用于XHTML)。如果系统检测到HTML中的任何codeS是无效的,它标志着XMPP.msg.Html为空白,所以我知道上面的方法是行不通的。

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.

谢谢!

推荐答案

会建议使用任何类似的TinyMCE或HtmlAgilityPack(可作为的NuGet包或codePLEX)。

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

TinyMCE的允许用户执行适当的格式控制富文本编辑和将输出结果HTML。

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

HtmlAgilityPAck,另一方面是一个图书馆,让你在你的方法所产生的HtmlStream通过,并输出这是一个有效的XHTML流。

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如下面这个工作粗糙例如:

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

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());

这篇关于C#HTML从web浏览器,以有效的XHTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆