简体   繁体   中英

C# linq to xml, delete tag name based on next tag

Delete tag New and its content if the next tag is <b>

here is my xml file

<tag>
    <New>some content</New>
    <b> bold </b>
    <New> content two </New>
    <p> p tag </p>
</tag>

output is

<tag>
    <b> bold </b>
    <New> content two </New>
    <p> p tag </p>
</tag>

here is my code

XElement rootImg = XElement.Parse(xml string variable);

IEnumerable<XElement> img =
    from el in rootImg.Descendants("New").ToList()
    select el;

foreach (XElement el in img)
{
    //what am i going to do here?
}
foreach (XElement el in img.ToArray())
{
    var afterElement = el.NodesAfterSelf().FirstOrDefault() as XElement;
    if (afterElement != null && afterElement.Name == "b")
    {
        el.Remove();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM