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" ?>
<InterfaceList>
  <Interface>
    <InterfaceName>Account Lookup</InterfaceName>
    <RequestXSD>ALREQ.xsd</RequestXSD>
    <ResponseXSD>ALRES.xsd</ResponseXSD>
  </Interface>
  <Interface>
    <InterfaceName>Balance Inquiry</InterfaceName>
    <RequestXSD>BIREQ.xsd</RequestXSD>
    <ResponseXSD>BIRES.xsd</ResponseXSD>
  </Interface>
</InterfaceList>

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?

Answers


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

Drawbacks to running Django under PyPy?

python django pypy

I understand that Django, PyPy, and Psycopg2 all work correctly together, and speed.pypy.org claims great performance improvements over CPython. Are there any downsides?

How do I search in files (for example in Java files) in Vista?

java search windows-vista

Is there a way or tool for Vista I can use to search for content in Java files? (I do not have an Eclipse project set up for it)

DataSource pattern versus setting Properties while Configuring Objects

objective-c ios oop

I often get confused with when to use DataSource Pattern and when to use the Properties for providing configuration information to objects.

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.