0

Could anyone please help to parse Html with Agility pack into a single string ?

I'm trying to parse Html similar to following format,

<blockquote>\n <p>Here is the first collection:<\/p>\n <ol>\n <li>List1<\/li>\n <li>List2<\/li>\n <li>List3<\/li>\n <\/ol>\n <p>Here is the second collection:<\/p>\n <ol>\n <li>List1<\/li>\n <li>List2<\/li>\n <\/ol>\n <\/blockquote> 

I try to use following method to get "p" and "li" and "blockquote". However, method .Descendants creates individual collections for "p", "li", and "blockquote", but I need to put individual element in sequence and store them in a single string.

 IEnumerable<HtmlNode> h3Tags = document.DocumentNode.Descendants("p"); foreach (var h3tag in h3Tags) {} 

for instance, I want my string stores, "Here is the first collection: List1 List2 List3 Here is the second collection List1 List2".

Thank you!

1 Answer 1

2

Use the InnerText property of the blockquote node. That should return the strings in expected order.

Do something like

var blockQuoteNode = document.DocumentNode.Descendants("blockquote").First(); // or do a document.DocumentNode.SelectSingleNode(//put the exact xpath value of the blockquote element here...) var stringsYouNeed = blockQuoteNode.InnerText; 
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, I make the question more clear. What if I have blockquote contains multiple "p" and "li"? because .First() only returns the very first node. Thanks!
The .First() will return the entire first blockquote node object, including all its child elements. These child elements are just other nodes that could be of type p and li. Keep in mind that the .First() method is just used to access the first node in a collection of descendant nodes (in this case, blockquote) in the document root node. You can specify which node or specify a specific xpath value to select the blockquote node that you want. Also if you give it a try, you'll notice that the InnerText property returns all the inner text property values of the node and of its child nodes.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.