您的位置:首页 > Web前端 > HTML

使用SgmlReader将HTML转换为合法的XML

2009-05-02 22:32 316 查看
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;


}


}


}


}

其他相关资料:http://www.kaiyuan8.org/Article/KLNvXMQhmKeyQyVGCfBZ.aspx

http://www.eggheadcafe.com/articles/20030317.asp
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: