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

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

2
  • 1
    Possible duplicate of stackoverflow.com/questions/5183385/… Commented Aug 23, 2013 at 9:47
  • 3
    Why bother with a HtmlDocument at all when you have the HTML in a string already? Commented Aug 23, 2013 at 9:47

6 Answers 6

12
string s = doc.DocumentNode.OuterHtml;

or

var sw = new StringWriter();
doc.Save(sw);
var s = sw.ToString();
Sign up to request clarification or add additional context in comments.

Comments

2

how about

string  html = doc.DocumentNode.OuterHtml;

Comments

0

OuterHTML will have the Entire HTML..

string s = doc.DocumentNode.OuterHtml

Comments

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.

Comments

0
string variableName = doc.DocumentNode.OuterHtml;

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.