如何使用xmlwriter将html文件转换为xml [英] how to convert a html file into xml using xmlwriter

查看:64
本文介绍了如何使用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[^]


这篇关于如何使用xmlwriter将html文件转换为xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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