我想知道是否有办法将结果列表与linq to xml结合到列表中。如果我会有以下xml例如:c#linq to xml to list
<?xml version="1.0"?>
<Sports xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SportPages>
<SportPage type="test">
<LinkPage>
<IDList>
<string>1</string>
<string>2</string>
</IDList>
</LinkPage>
</SportPage>
</SportPages>
</Sports>
我怎么能从IDList中获取字符串列表?
我是相当新的LINQ到XML,所以我只是尝试一些东西出来,我目前在这一点上:
var IDs = from sportpage in xDoc.Descendants("SportPages").Descendants("SportPage")
where sportpage.Attribute("type").Value == "Karate"
select new
{
ID = sportpage.Element("LinkPage").Element("IDList").Elements("string")
};
但VAR是混乱体面地阅读。没有办法,我可以从这里得到一个字符串列表?
感谢
这会抛出如果你有一个没有'type'属性的'SportPage'元素,则为一个对象null异常。 – 2010-04-08 14:38:51
解决了它,我删除了LinkPage后裔和SportPages后裔,因为感谢其他答复我想出了它autosearches :)所以非常感谢你的所有! – WtFudgE 2010-04-08 14:48:43