Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将html文档保存为字符串?_C#_Data Conversion_Dom - Fatal编程技术网

C# 如何将html文档保存为字符串?

C# 如何将html文档保存为字符串?,c#,data-conversion,dom,C#,Data Conversion,Dom,如何将其保存为字符串而不是Console.Out HtmlDocument doc = new HtmlDocument(); doc.Load(yourhtml); doc.Save(Console.Out); 或 怎么样 var sw = new StringWriter(); doc.Save(sw); var s = sw.ToString(); OuterHTML将包含整个HTML string variableName = doc.DocumentNode.Ou

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

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

怎么样

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

OuterHTML将包含整个HTML

string variableName = doc.DocumentNode.OuterHtml;
为什么不使用这个:

string s = doc.DocumentNode.OuterHtml

它将把html文档读取为字符串,而不初始化HtmlDocument对象。
yourHtml
真的是一个html还是一个路径?HtmlAgilityPack.HtmlDocument不包含接受html的Load方法。

如果字符串中已经有html,为什么还要麻烦使用
HtmlDocument
呢?
var str = File.ReadAllText(yourHtml);
string s = doc.DocumentNode.OuterHtml
var str = File.ReadAllText(yourHtml);
   HtmlDocument doc = new HtmlDocument();
   // call one of the doc.LoadXXX() functions
   Console.WriteLine(doc.DocumentNode.OuterHtml);