会员
周边
众包
新闻
博问
闪存
赞助商
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
会员中心
简洁模式
...
退出登录
注册
登录
代码改变世界
Cnblogs
Dashboard
Login
Home
Contact
Gallery
Subscribe
RSS
Cat in dotNET
使用SgmlReader将HTML转换为合法的XML
2007-02-06 14:51
Cat Chen
阅读(
4381
) 评论(
3
)
编辑
收藏
举报
public
static
string
Convert(
string
html)
{
if
(
string
.IsNullOrEmpty(html.Trim()))
{
return
string
.Empty;
}
using
(SgmlReader reader
=
new
SgmlReader())
{
reader.DocType
=
"
HTML
"
;
reader.InputStream
=
new
StringReader(html);
using
(StringWriter stringWriter
=
new
StringWriter())
{
using
(XmlTextWriter writer
=
new
XmlTextWriter(stringWriter))
{
reader.WhitespaceHandling
=
WhitespaceHandling.None;
writer.Formatting
=
Formatting.Indented;
XmlDocument doc
=
new
XmlDocument();
doc.Load(reader);
if
(doc.DocumentElement
==
null
)
{
return
string
.Empty;
}
else
{
doc.DocumentElement.WriteContentTo(writer);
}
writer.Close();
string
xhtml
=
stringWriter.ToString();
return
xhtml;
}
}
}
}
刷新页面
返回顶部
About