Does HtmlAgilityPack have the ability to return the whole HTML markup from an HtmlDocument object as a string?
- Why do you need to return the whole markup as a string, when that's the input to something that parses it?Matt Ball– Matt Ball2011-03-03 16:16:44 +00:00Commented Mar 3, 2011 at 16:16
- I am trying to save the markup directly to a word document ( .doc ) file.deostroll– deostroll2011-03-03 16:20:04 +00:00Commented Mar 3, 2011 at 16:20
- 2Possible duplicate of HtmlAgility - Save parsing to a stringAmit G– Amit G2016-07-06 09:30:33 +00:00Commented 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!BrainSlugs83– BrainSlugs832016-10-12 22:47:47 +00:00Commented Oct 12, 2016 at 22:47
Add a comment |
2 Answers
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.
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
buda
With this you have whole HTML, and you can save it wherever you want.
deostroll
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...
BrainSlugs83
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.