2010-04-21 106 views
0

我的XML是從哪裏來的,我必須取出一本書詳細刪除XML標籤C#

<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> 

現在我要與bookid == Cshar001刪除圖書詳細信息, 請讓我知道任何的LINQ命令使用將搜索並從XML中只刪除該標籤。

+0

看起來這是一門功課 – 2010-04-21 10:22:03

回答

1

可以使用XNode.Remove()方法爲:

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"); 
+0

其中x.Element( 「BOOKID」)== 「Cshar001」 選擇x在這一行給錯誤 – Vijay 2010-04-21 11:09:56

+0

對不起關於這一點,忘了'.Value'。 – Prutswonder 2010-04-21 11:37:13