使用LINQ-to-XML选择元素

时间:2010-12-08 23:52:01

标签: 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</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>

我创建了这个查询,但它没有编译:

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

它强调第一行(约from部分),说错误是:

  

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

怎么了?

1 个答案:

答案 0 :(得分:1)

添加using System.Linq;