关于C#:如何将html文档保存到字符串中?

How to save html doc in to a string?

1
2
3
 HtmlDocument doc = new HtmlDocument();
    doc.Load(yourhtml);
    doc.Save(Console.Out);

如何将其保存为字符串而不是Console.Out


1
string s = doc.DocumentNode.OuterHtml;

1
2
3
var sw = new StringWriter();
doc.Save(sw);
var s = sw.ToString();

怎么样

1
string  html = doc.DocumentNode.OuterHtml;

1
2
3
   HtmlDocument doc = new HtmlDocument();
   // call one of the doc.LoadXXX() functions
   Console.WriteLine(doc.DocumentNode.OuterHtml);

为什么不使用它:

1
var str = File.ReadAllText(yourHtml);

它将在不初始化HtmlDocument对象的情况下将您的html文档读取为字符串。 yourHtml确实是html还是只是路径? HtmlAgilityPack.HtmlDocument不包含接受html的Load方法。


OuterHTML将具有整个HTML。.

1
string s = doc.DocumentNode.OuterHtml

1
string variableName = doc.DocumentNode.OuterHtml;