【问题标题】:How to save html doc in to a string?如何将html doc保存到字符串中?
【发布时间】:2023-03-22 10:53:01
【问题描述】:
 HtmlDocument doc = new HtmlDocument();
    doc.Load(yourhtml);
    doc.Save(Console.Out);

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

【问题讨论】:

标签: c# data-conversion dom


【解决方案1】:
string s = doc.DocumentNode.OuterHtml;

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

【讨论】:

    【解决方案2】:

    怎么样

    string  html = doc.DocumentNode.OuterHtml;
    

    【讨论】:

      【解决方案3】:

      OuterHTML 将拥有整个 HTML..

      string s = doc.DocumentNode.OuterHtml
      

      【讨论】:

        【解决方案4】:

        为什么不使用这个:

        var str = File.ReadAllText(yourHtml);
        

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

        【讨论】:

          【解决方案5】:
          string variableName = doc.DocumentNode.OuterHtml;
          

          【讨论】:

            【解决方案6】:
               HtmlDocument doc = new HtmlDocument();
               // call one of the doc.LoadXXX() functions
               Console.WriteLine(doc.DocumentNode.OuterHtml);
            

            【讨论】:

              猜你喜欢
              • 2016-09-17
              • 2013-11-08
              • 1970-01-01
              • 2016-10-16
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多