Dejan Stuparic
Dejan Stuparic

Reputation: 595

Selecting elements using LINQ-to-XML

I have this XML file:

<?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>

and I create this query, but it does not compile:

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

It underlines the first line (around from section), saying the error is:

'Where' not found, are you missing a reference to System.Core.dll or using directive for System.Linq

What's wrong?

Upvotes: 1

Views: 457

Answers (1)

SLaks
SLaks

Reputation: 888117

Add using System.Linq;

Upvotes: 1

Related Questions