Ofer Gozlan Ofer Gozlan - 3 years ago 274
C# Question

Get div content by id

I have a string that keeps entire html document. I would like to get all the content inside a div with specific id. For example:

<div id="myId" class = "myClass">
<div class = "myClass">hello</div>

I need the content between the tag with id="myId" and it's closing tag.
Any way to achieve this? The output should be the second line.

Answer Source

The clean and correct way would be via an HTML parser, like HtmlAgilityPack:

string stringThatKeepsYourHtml = "<div id=....";
HtmlDocument doc = new HtmlDocument();
string whatUrLookingFor = doc.GetElementbyId("myId").InnerHtml;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download