81
votes

Does HtmlAgilityPack have the ability to return the whole HTML markup from an HtmlDocument object as a string?

2
Why do you need to return the whole markup as a string, when that's the input to something that parses it? - Matt Ball
I am trying to save the markup directly to a word document ( .doc ) file. - deostroll
@MattBall because HTML Agility Pack isn't read only and it's not just for parsing! It allows you to make changes to the HTML elements. It's only natural that you would then want to be able to get the final HTML back out! - BrainSlugs83

2 Answers

143
votes

Sure, you can do like this:

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

OuterHtml contains the whole html.

-5
votes

You can create WebRequest passing Url and Get webResponse . Get ResponseStream from WebResponse and read it into a String.

string result = string.Empty;

WebRequest req = WebRequest.Create(Url);
WebResponse res= wrq.GetResponse();    
StreamReader reader = new StreamReader(res.GetResponseStream());
result = reader.ReadToEnd();    
reader.Close();
res.Close();

Hope this helps.