c# – HtmlAgilityPack:如何创建缩进的HTML?

前端之家收集整理的这篇文章主要介绍了c# – HtmlAgilityPack:如何创建缩进的HTML?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
所以,我使用 HtmlAgilityPack生成html并且它工作正常,但是html文本没有缩进.我可以得到缩进的XML,但我需要HTML.有办法吗?
HtmlDocument doc = new HtmlDocument();

// gen html
HtmlNode table = doc.CreateElement("table");
table.Attributes.Add("class","tableClass");
HtmlNode tr = doc.CreateElement("tr");
table.ChildNodes.Append(tr);
HtmlNode td = doc.CreateElement("td");
td.InnerHtml = "—";
tr.ChildNodes.Append(td);

// write text,no indent :(
using(StreamWriter sw = new StreamWriter("table.html"))
{
        table.WriteTo(sw);
}

// write xml,nicely indented but it's XML!
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Indent = true;
settings.ConformanceLevel = ConformanceLevel.Fragment;
using (XmlWriter xw = XmlTextWriter.Create("table.xml",settings))
{
        table.WriteTo(xw);
}

解决方法

据我所知,HtmlAgilityPack无法做到这一点.但你可以通过类似问题提出的html整洁包看看:

> Html Agility Pack: make code look
neat

> Which is the best HTML tidy pack? Is there any option in HTML agility pack to make HTML webpage tidy?

原文链接:https://www.f2er.com/csharp/91372.html

猜你在找的C#相关文章