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 <body> tags.

For example:

Sample Input:

<html>
   <head>
     <title>blah</title>
   </head>
   <body>
    <p>My content</p>
   </body>
</html>

Desired Output:

<p>My content</p>

Ideally, I'd like to return the content unaltered if it didn't contain an <html> or <body> element (eg. assume that I was passed a fragment in the first place if it wasn't a full html document)

Can anyone point me in the right direction?

Answers


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 :)


Need Your Help

SQL recursive logic

sql sql-server recursion common-table-expression

I have a situation where I need to configure existing client data to address a problem where our application was not correctly updating IDs in a table when it should have been.

Java Concurrency: Is cancelling Futures necessary for them to be Garbage Collected?

java garbage-collection future

I am writing some code where I might need to create an unbounded number of future objects (java.util.concurrent.Future).