繁体   English   中英

使用LINQ-to-XML选择元素

[英]Selecting elements using 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使用指令

怎么了?

using System.Linq;添加using System.Linq;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM