How to delete specific nodes from an XElement?

后端 未结 6 438
时光取名叫无心
时光取名叫无心 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:12

    You can try this approach:

    var nodes = xRelation.Elements().Where(x => x.Element("Conditions") != null).ToList();
    
    foreach(var node in nodes)
        node.Remove();
    

    Basic idea: you can't delete elements of collection you're currently iterating.
    So first you have to create list of nodes to delete and then delete these nodes.

提交回复
热议问题