How to get all values inside a div as a string using HtmlAgilityPack

c# html html-agility-pack

Question

I have an html that i need to extract just a div and the elements inside this div but as a string.

 <div id=01>
    <div id-02>
      <label></labell>
      <input></input>
       //etc
     </div>
 </div>

How do i get all inside the div 01 as a single string?

The only way i found is to get using the nodes, but it returns me a collection of HtmlNodes.

document.DocumentNode.SelectNodes("//*[contains(@class,'container')]").Descendants("div").Select(p => p.OuterHtml);

Popular Answer

var Node = document.DocumentNode.SelectNodes("//div[@id='01']").SingleOrDefault();
var target = Node.InnerHtml;

becouse id is single I used SingleOrDefault for this id




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