Does HtmlAgilityPack have the ability to return the whole HTML markup from an HtmlDocument object as a string?
Asked
Active
Viewed 5.3k times
87
-
Why do you need to return the whole markup as a string, when that's the input to something that parses it? – Matt Ball Mar 03 '11 at 16:16
-
I am trying to save the markup directly to a word document ( .doc ) file. – deostroll Mar 03 '11 at 16:20
-
2Possible duplicate of [HtmlAgility - Save parsing to a string](http://stackoverflow.com/questions/5107302/htmlagility-save-parsing-to-a-string) – Amit G Jul 06 '16 at 09: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! – BrainSlugs83 Oct 12 '16 at 22:47
2 Answers
152
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.

Simon Mourier
- 132,049
- 21
- 248
- 298
-
3
-
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. – NoChance Jul 27 '17 at 01:12
-
-6
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.

buda
- 2,372
- 1
- 18
- 23
-
-
2I'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... – deostroll Mar 03 '11 at 17:28
-
6Downvoted. 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. – BrainSlugs83 Oct 12 '16 at 21:41