remove a duplicate element(with specific value) from xml using linq

Posted by Q8Y on Stack Overflow See other posts from Stack Overflow or by Q8Y
Published on 2012-06-13T09:36:48Z Indexed on 2012/06/13 10:40 UTC
Read the original article Hit count: 190

Filed under:
|
|

If I have this xml

<?xml version="1.0" encoding="utf-8"?>
<super>
  <A value="1234">
    <a1 xx="000" yy="dddddd" />
    <a1 xx="111" yy="eeeeee" />
    <a1 xx="222" yy="ffffff"/>
  </A>
</super>

and I need to remove a1 element (that have xx=222) completely. why this won't happen using my code?? i realized that it will delete it only if it was placed the first element(i.e, if i want to delete a1 that have x=000 , it will delete it since its the first one), why is that??

what wrong with the code ??

var employee = from emp in element.Elements("A")
    where (string)emp.Element("a1").Attribute("xx") == "222"
    select emp.Element("a1");

foreach (var empployee_1 in employee)
{
    empployee_1.Remove();
}

element.Save(@"TheLocation");

thanks alot

© Stack Overflow or respective owner

Related posts about c#

Related posts about Xml