How to delete specific nodes from an XElement?

拥有回忆 提交于 2019-12-10 02:36: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.

I create a for loop as below but it does not delete my nodes

foreach (XElement xx in xRelation.Elements())
{
  if (xx.Element("Conditions") != null)
  {
    xx.Remove();
  }
}

Sample:

<Rules effectNode="2" attribute="ability" iteration="1">
    <Rule cause="Cause1" effect="I">
      <Conditions>
        <Condition node="1" type="Internal" />
      </Conditions>
    </Rule>
    <Rule cause="cause2" effect="I">
      <Conditions>
        <Condition node="1" type="External" />
      </Conditions>
    </Rule>
</Rules>

How can I remove all the "Rule" nodes if they contain "conditions" node?


回答1:


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.




回答2:


You can use Linq:

xRelation.Elements()
     .Where(el => el.Elements("Conditions") == null)
     .Remove();

Or create a copy of the nodes to delete, and delete them after (in case the first method doesn't work):

List nodesToDelete = xRelation.Elements().Where(el => el.Elements("Conditions") == null).ToList();

foreach (XElement el in nodesToDeletes)
{
    // Removes from its parent, but not nodesToDelete, so we can use foreach here
    el.Remove();
}



回答3:


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();
}



回答4:


passiveLead.DataXml.Descendants("Conditions").Remove();



回答5:


var el = xRelation.XPathSelectElement("/Rules/Rule/Conditions");
while (el != null)
{
      el.Remove();
      el = xRelation.XPathSelectElement("/Rules/Rule/Conditions");
}



回答6:


just an idea:

Reverse the Linq "condition" and You will get a List without "Rule" nodes



来源:https://stackoverflow.com/questions/28169101/how-to-delete-specific-nodes-from-an-xelement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!