如何使用xmlwriter将html文件转换为xml

问题描述:


我想使用xmlwriter将html代码转换为xml.我该怎么做.
请帮忙.



感谢和问候
khan

Hi
I want to convert the html code into xml using xmlwriter. how can i do this.
Please help.



thanks and regards
khan

你为什么要不能这样做吗?但是好吧,我认为您可以使用此代码进行操作:

Why do you wan''t to do that? But ok, you can use this code to do it i think:

var html = "<html><head></head><body><h1>Hello world</h1><p>This is a paragraph</p></body></html>";

// Easiest way is to go through a XmlDocument
var xml = new XmlDocument();
xml.LoadXml(html); // now you have a XmlDocument holding all the html

var sw = new StringWriter();
var writer = XmlWriter.Create(sw);
// Then write the XmlDocument to the XmlWriter. 
xml.WriteTo(writer);
// Now the XmlDocument is written to the XmlWriter (writer).



如果html无效xml,那么这当然就行不通了!



This will off course not work if the html is not valid xml!


下面的链接可以为您提供帮助:
将HTML转换为XHTML并清除不必要的标记和属性 [ ^ ]
Below link can help you:
Convert HTML to XHTML and Clean Unnecessary Tags and Attributes[^]