0

For examle:

<div id="outer">
    <div id="a">
        <div class="b"> 11111111111</div>
        <div class="b"> 22222222222222</div>
    </div>
</div>

Now I want to match the elements of id is a, and replace it to empty, but I found I can't, because id="a" is not the outer div. This is my c# code ,it will match the last Tag.

Regex regex = new Regex(@"<div id=""a([\s\S]*) (<\/[div]>+)");
NoName
  • 7,940
  • 13
  • 56
  • 108
MapleStory
  • 628
  • 3
  • 11
  • 22

1 Answers1

1

Try this:

var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);

var divs = doc.DocumentNode.Descendants().Where(x => x.Name == "div" && x.Id == "a");

foreach (var div in divs.ToArray())
{
    div.InnerHtml = "";
}

var result = doc.DocumentNode.OuterHtml;

The result I get is:

<div id="outer">
    <div id="a"></div>
</div>
Enigmativity
  • 113,464
  • 11
  • 89
  • 172