Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linq to XML查询返回类的空列表_Xml_Linq To Xml - Fatal编程技术网

Linq to XML查询返回类的空列表

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

我想使用Linq来帮助制作一个演员参与的电影列表。我当前正在尝试使用此查询:

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();