我想根据条件查找xml的一部分,因此我使用linq to XML。不幸的是结果总是为空,所以我想我做错了。下面显示的是我正在解析的XML示例。使用Linq找到基于ID的特定XML元素
<Stuff>
<ItemsA />
<ItemsB />
<ItemsC>
<Item xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Stuff">
<Id>4bd7b5ac-cb29-4d34-97be-deaebe4a5186</Id>
<Children>
<Item>
<Id>22e3ef6b-4321-40c3-9237-196ba527e9ad </Id>
<Name>SomeName</Name>
</Item>
</Children>
<Description>SomeText</Description>
<Name>NewName</Name>
</Item>
我正在寻找的“ItemsC”部分,其中可以存在多个“项” XML的块在(只有一个在该示例中示出)。我想根据它的“Id”检索元素,在上面的例子中,Id是“4bd7b5ac-cb29-4d34-97be-deaebe4a5186”。
我已经使用的代码如下所示:
的XElement数据= XElement.Parse(的GetFile());
var pbp = (from b in data.Descendants("Item")
where b.Element("Id").Value == "4bd7b5ac-cb29-4d34-97be-deaebe4a5186"
select b.Element("Id").Parent).FirstOrDefault();
pbp始终返回为空。任何人都可以帮助我产生正确的linq表达。
谢谢,改变现在的作品。时间让我在Linq上做更多的阅读:) – Retrocoder 2009-10-20 10:39:09