C# C linq转换为xml,根据下一个标记删除标记名

C# C linq转换为xml,根据下一个标记删除标记名,c#,linq,C#,Linq,删除新标记及其内容(如果下一个标记为 这是我的xml文件 <tag> <New>some content</New> <b> bold </b> <New> content two </New> <p> p tag </p> </tag> 输出为 <tag> <b> bold </b> <

删除新标记及其内容(如果下一个标记为

这是我的xml文件

<tag>
    <New>some content</New>
    <b> bold </b>
    <New> content two </New>
    <p> p tag </p>
</tag>
输出为

<tag>
    <b> bold </b>
    <New> content two </New>
    <p> p tag </p>
</tag>
这是我的密码

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里把它加到你的foreach里
foreach (XElement el in img.ToArray())
{
    var afterElement = el.NodesAfterSelf().FirstOrDefault() as XElement;
    if (afterElement != null && afterElement.Name == "b")
    {
        el.Remove();
    }
}