tags:

views:

76

answers:

1

I am having on xml from which i have to remove the one book detail

<Books>
  <bookdetail>
    <bookname>ThreeIdiot</bookname>
    <bookauthor>chetan bhagat</bookauthor>
    <bookid>idi001</bookid>
    <isavailable>true</isavailable>
    <subscribername>NA</subscribername>
  </bookdetail>
  <bookdetail>
    <bookname>Csharp</bookname>
    <bookauthor>Kanitker</bookauthor>
    <bookid>Cshar001</bookid>
    <isavailable>true</isavailable>
    <subscribername>NA</subscribername>
  </bookdetail>
  <bookdetail>
    <bookname>VBbasic</bookname>
    <bookauthor>Andrew Parker</bookauthor>
    <bookid>idi001</bookid>
    <isavailable>true</isavailable>
    <subscribername>NA</subscribername>
  </bookdetail>
</Books>

Now I have to remove the book detail with bookid == Cshar001, Please let me know any Linq command using will search and remove only that tag from the XML.

+1  A: 

You can use the XNode.Remove() method for that:

var xDoc = XDocument.Load("myfilename.xml");
var xElement = (
    from x in xDoc.Root.Elements("bookdetail")
    where x.Element("bookid").Value == "Cshar001"
    select x
    ).FirstOrDefault();
xElement.Remove();
xDoc.Save("myfilename.xml");
Prutswonder
where x.Element("bookid") == "Cshar001" select x giving error on this line
Vijay
Sorry about that, forgot the `.Value`.
Prutswonder