Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/25.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
C# 使用LINQ到XML选择元素_C#_Linq To Xml - Fatal编程技术网

C# 使用LINQ到XML选择元素

C# 使用LINQ到XML选择元素,c#,linq-to-xml,C#,Linq To Xml,我有一个XML文件: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <games> <game id="123456" name="501"> <player id="1"> <name>john</name> <score>495</score> <movesLeft>15&l

我有一个XML文件:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<games>
  <game id="123456" name="501">
    <player id="1">
      <name>john</name>
      <score>495</score>
      <movesLeft>15</movesLeft>
      <won>No</won>
      <completed>uncompleted</completed>
    </player>
    <player id="2">
      <name>
         konj
      </name>
      <score>501</score>
      <movesLeft>15</movesLeft>
      <won>No</won>
      <completed>uncompleted</completed>
    </player>
  </game>
</games>
它在第一行(在部分的
周围)加下划线,表示错误为:

找不到“Where”,是否缺少对System.Core.dll的引用或使用System.Linq指令


怎么了?

使用System.Linq添加

使用System.Linq添加

谢谢,我太傻了。我添加了XLinq,我想这已经足够了。过了一段时间我又回到了这个项目,但从来没有解决过一个问题。此查询从未执行,当我尝试使用时,我得到
NullReferenceException
<代码>我尝试了Console.WriteLine(query.ToString)但它从未到达那里。这个查询有什么问题,我仍然使用上面的xml文件。
f.Element(“name”)是
null
,因为
`标记只包含
元素。谢谢,我太傻了。我添加了XLinq,我想这已经足够了。过了一段时间我又回到了这个项目,但从来没有解决过一个问题。此查询从未执行,当我尝试使用时,我得到
NullReferenceException
<代码>我尝试了Console.WriteLine(query.ToString)但它从未到达那里。这个查询有什么问题,我仍然使用上面的xml文件。
f.Element(“name”)是
null
,因为
`标记只包含
元素。
string path = @"D:\xml\dartDatabase.xml";
XElement file = XElement.Load(path);

var query = from f in file.Element("games").Elements("game")
            where (string)f.Attribute("id") == "123"
            select (string)f.Element("name");