How to write simple query to XElement?

Trying to use Linq to XML for the first time and having some problems. I have this XML file that needs to be read and used for various tasks. The file contains a list of entities called 'interfaces'. To start with I want to display a list of names of these interfaces.

Here is the XML file:

<?xml version="1.0" encoding="utf-8" ?>
    <InterfaceName>Account Lookup</InterfaceName>
    <InterfaceName>Balance Inquiry</InterfaceName>

Here is the query code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Stub {
    public class InterfaceList : XElement {

        public void GetInterfaceNameList() {
            var v = from interface in this.Elements("Interface")
                select interface.Element("InterfaceName").Value;

The idea is to load InterfaceList from the file, and then to use it to query any I may need. The problem is that I'm getting error message for everything in the query. here are a few of them:

  • Error 14 The name 'from' does not exist in the current context
  • Error 15 The type or namespace name 'select' could not be found (are you missing a using directive or an assembly reference?) Error
  • Error 16 'System.Xml.Linq.XElement.Value' is a 'property' but is used like a 'type'

What's wrong here?


If you want to call your variable 'interface' (which is a reserved word) you will need to escape it, like this:

var v = from @interface in this.Elements("Interface")
        select @interface.Element("InterfaceName").Value;

Probably better to just rename it though....

Need Your Help

SpriteKit SKAction group is running, but the SKSpriteNode image isn't being affected

sprite-kit skspritenode skaction

I have created an SKAction group that alters the SKSpriteNode image (alpha and rotation) whilst playing a sound effect. However, when I runAction: the sound effect is played, but the image remains ...

T-SQL Correct Join approach for 2 tables when right table has nulls

sql sql-server

Feel like a dufus and missing something simple. Left table is Employee, with a column called EmployeeCode. Right table is Invoices, with a column called SalesPerson, and a TotalAmount column which ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.