Linq to XML查询返回类的空列表
我想使用Linq来帮助制作一个演员参与的电影列表。我当前正在尝试使用此查询:Linq to XML查询返回类的空列表,xml,linq-to-xml,Xml,Linq To Xml,我想使用Linq来帮助制作一个演员参与的电影列表。我当前正在尝试使用此查询: XDocument doc = XDocument.Load(reqUrl); var items = (from c in doc.Descendants("filmography") select new Actor { Filmogr
XDocument doc = XDocument.Load(reqUrl);
var items = (from c in doc.Descendants("filmography")
select new Actor
{
Filmograpgy = c.Element("link").Value,
}).ToList<Actor>();
你需要把钥匙拔出来。Value将获取链接标记Value
之间的值
你需要:
c.Element("link").Attribute("href")
此外,您仅获得一个值,因为您正在基于根节点的选择(从影记录)执行选择
问题在于,您的代码假设只有一个链接元素,但有很多链接元素,而且您可能不希望看到它的值(为空),例如title属性:
Filmography = c.Elements("link")
.Select(x => x.Attribute("title").Value)
.ToList();
上面假定从影记录
属性是包含电影标题的列表
。考虑到您没有显示Actor
的类定义以及您实际想要查询的数据,我只能这样做了
<filmography>
<link>THIS WILL GET PICKED IN THE ORIGINAL QUERY</link>
<link>THIS WILL BE IGNORED</link>
<link>THIS WILL BE IGNORED</link>
<link>THIS WILL BE IGNORED</link>
</filmography>
c.Elements("link")
.Select(c=>c.Attribute("href").Value)
.ToList();
Filmography = c.Elements("link")
.Select(x => x.Attribute("title").Value)
.ToList();