How to delete specific nodes from an XElement?

后端 未结 6 403
时光取名叫无心
时光取名叫无心 2021-01-11 18:49

I have created a XElement with node which has XML as below.

I want to remove all the \"Rule\" nodes if they contain \"conditions\" node.

6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-11 19:14

    I've made a small example for you:

    XDocument document = XDocument.Parse(GetXml());
    var rulesNode = document.Element("Rules");
    if (rulesNode != null)
    {
        rulesNode.Elements("Rule").Where(r => r.Element("Conditions") != null).Remove();
    }
    

提交回复
热议问题