XML : Find list of xml elements that have a list of names using linq (and remove them)

I have the following XML

  <itemList>    <Item>      <shipAddress></shipAddress>      <shipMethod></shipMethod>      <rate>50.00</rate>      <custom>          <custcol_1>55</custcol_1>          <custcol_2>60</custcol_2>      </custom>      <special>         <spec1>A</special1>      </special>    </Item>    <Item>       <shipAddress></shipAddress>       <rate>50.00</rate>       <custom>          <custcol_1>7889</custcol_1>          <custcol_2>754</custcol_2>       </custom>   </Item>      

What I am trying to do is gather a list any nodes that are <custom> or <special> because I want to remove them.

I tried to do that with the following but it didnt get any nodes:

  nodesToRemove = xeConsolItm.Elements()      .Where(itm =>      (          itm.Element("custom") != null &&          itm.Element("special") != null &&          itm.Element("other") != null      ))      .ToList();      foreach (var xElement in nodesToRemove)  {      nodesToRemove.Remove();  }    

I also tried the same with || but that didn't work either. In both cases the nodesToRemove ends up empty.

I wondered a way to do that. I have been able to do it by removing each at a time but when I try to merge the 3 together, I have no luck. Is the best way to just do one at a time?

No comments:

Post a Comment