HtmlAgilityPack:如何创建HTML缩进? [英] HtmlAgilityPack: how to create indented HTML?
本文介绍了HtmlAgilityPack:如何创建HTML缩进?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以,我使用HtmlAgilityPack生成HTML,它的工作完美,但HTML文本不缩进。我可以缩进的XML不过,但我需要的HTML。 ?有没有办法
的HTMLDocument DOC =新的HTMLDocument();
//根HTML
HtmlNode表= doc.CreateElement(表);
table.Attributes.Add(类,tableClass);
HtmlNode TR = doc.CreateElement(TR);
table.ChildNodes.Append(TR);
HtmlNode TD = doc.CreateElement(TD);
td.InnerHtml =&放大器; MDASH;;
tr.ChildNodes.Append(TD);
//,没有缩进:(
使用书写文本(StreamWriter的SW =新的StreamWriter(table.html))
{
table.WriteTo(SW );
}
//编写XML,很好地缩进但它的XML
XmlWriterSettings设置=新XmlWriterSettings();!
settings.OmitXmlDeclaration = TRUE;
settings.Indent = TRUE;
settings.ConformanceLevel = ConformanceLevel.Fragment;
使用(XmlWriter的XW = XmlTextWriter.Create(table.xml设置))
{
table.WriteTo(XW);
}
解决方案
据我所知,HtmlAgilityPack不能做到这一点但你可以看看,通过它在类似的问题提出的HTML整洁包:
So, I am generating html using HtmlAgilityPack and it's working perfectly, but html text is not indented. I can get indented XML however, but I need HTML. Is there a way?
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);
}
解决方案
As far as I know, HtmlAgilityPack cannot do this. But you could look through html tidy packs which are proposed in similar questions:
- 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?
这篇关于HtmlAgilityPack:如何创建HTML缩进?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文