print webpage source from HtmlAgilityPack

beautifulsoup c# html html-agility-pack python

Question

I'm new to C# coming from a python background. I've had a hard time figuring this out. In python I can use beautifulsoup to do this:

from bs4 import BeautifulSoup
import requests
r = requests.get('http://stackoverflow.com').content
soup = BeautifulSoup(r).prettify
print soup

This gets the source content of stackoverflow.com, and prints it in to the console. I CANNOT figure out how to do this in C#.

        var webGet = new HtmlWeb();
        var document = webGet.Load("http://stackoverflow.com");
        Console.WriteLine(document);

This does not work, it only prints the type of var document. Does anyone know how to do this? It would be much appreciated - I'm trying to learn C# by converting one of my python programs, just fyi.

Accepted Answer

Use the OuterHtml property of the DocumentNode:

var webGet = new HtmlWeb();
var document = webGet.Load("http://stackoverflow.com");
Console.WriteLine(document.DocumentNode.OuterHtml);



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why