当前位置: 首页 > 工具软件 > SGMLReader > 使用案例 >

SgmlReader html to xml

百里胜泫
2023-12-01

SgmlReader

SgmlReader是一个开源的解析器,运用SgmlReader类来解析HTML文件,并生成格式规范(well-formed)的HTML。

主站地址为:http://archive.msdn.microsoft.com/SgmlReader

下面代码使用SgmlReader格式化HTML:

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

 

转载于:https://www.cnblogs.com/liaotongquan/archive/2012/12/11/2813115.html

 类似资料:

相关阅读

相关文章

相关问答