93

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

4
  • Why do you need to return the whole markup as a string, when that's the input to something that parses it? Commented Mar 3, 2011 at 16:16
  • I am trying to save the markup directly to a word document ( .doc ) file. Commented Mar 3, 2011 at 16:20
  • 2
    Possible duplicate of HtmlAgility - Save parsing to a string Commented Jul 6, 2016 at 9:30
  • 5
    @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! Commented Oct 12, 2016 at 22:47

2 Answers 2

157

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.

Sign up to request clarification or add additional context in comments.

2 Comments

When I tried this with the current release, I got the url back not the full page! No worries I will check, it must be me.
same here, did you find out a way that works for you?
-7

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.

3 Comments

With this you have whole HTML, and you can save it wherever you want.
I'd need to further work with the HTML, and then finally I'd need the final html document as string...I just want to know if the tool only allows me to save to a file, OR do I have the option of extracting the html out without the need to save/persist the file...
Downvoted. The question was specifically about the HTML Agility Pack, not about making a web request. -- This answer is completely unrelated to the question that was asked.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.