delete element from xml using LINQ

Posted by Shishir on Stack Overflow See other posts from Stack Overflow or by Shishir
Published on 2012-11-20T22:41:36Z Indexed on 2012/11/20 23:00 UTC
Read the original article Hit count: 848

Filed under:
|
|
|

Hello I've a xml file like:

<starting>
   <start>
      <site>mushfiq.com</site>
      <site>mee.con</site>
      <site>ttttt.co</site>
      <site>jkjhkhjkh</site>
      <site>jhkhjkjhkhjkhjkjhkh</site>
     <site>dasdasdasdasdasdas</site>
 </start>
</starting>

Now I need to delete any ... and value will randomly be given from a textbox.

Here is my code :

XDocument doc = XDocument.Load(@"AddedSites.xml");                
var deleteQuery = from r in doc.Descendants("start") where r.Element("site").Value == txt.Text.Trim() select r;
foreach (var qry in deleteQuery)
{
    qry.Element("site").Remove();
}
doc.Save(@"AddedSites.xml");

If I put the value of first element in the textbox then it can delete it, but if I put any value of element except the first element's value it could not able to delete! I need I'll put any value of any element...as it can be 2nd element or 3rd or 4th and so on.... can anyone help me out? thanks in advanced!

© Stack Overflow or respective owner

Related posts about c#

Related posts about Xml