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

How to save this into an string instead of Console.Out

Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70
ragmn
  • 495
  • 1
  • 8
  • 29

6 Answers6

12
string s = doc.DocumentNode.OuterHtml;

or

var sw = new StringWriter();
doc.Save(sw);
var s = sw.ToString();
I4V
  • 34,891
  • 6
  • 67
  • 79
2

how about

string  html = doc.DocumentNode.OuterHtml;
Damith
  • 62,401
  • 13
  • 102
  • 153
0
string variableName = doc.DocumentNode.OuterHtml;
Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
Nisarg Shah
  • 354
  • 1
  • 14
0

OuterHTML will have the Entire HTML..

string s = doc.DocumentNode.OuterHtml
Ravi Gadag
  • 15,735
  • 5
  • 57
  • 83
0

Why not using this:

var str = File.ReadAllText(yourHtml);

It will read your html document to string without initializing HtmlDocument object. Is yourHtmlreally a html or just a path? HtmlAgilityPack.HtmlDocument does not contain Load method accepting html.

Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
0
   HtmlDocument doc = new HtmlDocument();
   // call one of the doc.LoadXXX() functions
   Console.WriteLine(doc.DocumentNode.OuterHtml);
Deepika C P
  • 1,263
  • 2
  • 13
  • 23