Skip to content Skip to sidebar Skip to footer

Html Agility Pack - Get Html Fragment From An Html Document

Using the html agility pack; how would I extract an html 'fragment' from a full html document? For my purposes, an html 'fragment' is defined as all content inside of the

Solution 1:

I think you need to do it in pieces.

you can do selectnodes of document for body or html as follows

doc.DocumentNode.SelectSingleNode("//body") // returns body with entire contents :)

then you can check for null values for criteria and if that is provided, you can take the string as it is.

Hope it helps :)

Solution 2:

The following will work:

publicstringGetFragment(HtmlDocument document)
{
   return doc.DocumentNode.SelectSingleNode("//body") == null ? doc.DocumentNode.InnerHtml : doc.DocumentNode.SelectSingleNode("//body").InnerHtml;
}

Post a Comment for "Html Agility Pack - Get Html Fragment From An Html Document"