Linq to XML selecting children list

An example XML file is this:

<?xml version="1.0" encoding="UTF-8"?>
        <behavior id="Bullet" version="1">Bullet</behavior>
        <behavior id="Fade" version="1">Fade</behavior>
        <behavior id="Flash" version="1">Flash</behavior>
        <behavior id="Sin" version="1">Sine</behavior>
        <behavior id="scrollto" version="1">Scroll To</behavior>

I have the query:

var data = (from item in loaded.Descendants("game")
            select new
                name = item.Element("name").Value,
                behaviorlist = item.Element("behaviors-used").Value

Which seems to work fine. However, I need to now retrieve all the <behavior> elements in the behaviorlist. I can't seem to do it like this:

var bq = (from c in data.behaviorlist select new { behaviour = c.Element("behaviour")});

(Throws invalid syntax errors).

How do I retrive all the behaviours and not only access their text but also the properties id and version?


Your behaviorlist is not really a list right now - you want the elements not the combined text, so instead of using the Value property of the parent node, you should retrieve the Elements with name "behavior":

var data = (from item in loaded.Descendants("game")
            select new
                name = item.Element("name").Value,
                behaviorlist = item.Element("behaviors-used").Elements("behavior")

From the resulting list you can then easily retrieve the properties:

var bq = (from c in data.behaviorlist select new 
    id = c.Attribute("id").Value,
    version = c.Attribute("version").Value,

id and version are attributes of a behavior node:

var items =
             .Select((w, i) =>
                        Index = i,
                        Value = w.Value,
                        Id = w.Attribute("id").Value,
                        Version = w.Attribute("version").Value

Was able to get this to work in LinqPad with your document:

  var data = (from item in loaded.DescendantsAndSelf("game")
            select new{
                       name = item.Element("name").Value,
                       behaviorlist = item.Element("behaviours-used")

  var bq = (from c in data.behaviorlist.Descendants("behaviour")
            select new {
                         behaviour = c.Value,
                         id = c.Attribute("id").Value,
                         version = c.Attribute("version").Value

and yielded a sequence of 5 anonymous objects {behaviour, id, version}

Need Your Help

Use Django template tags in jQuery/Javascript?

javascript jquery django django-templates

Can I use Django's template tags inside Javascript? Like using {% form.as_p %} in jQuery to dynamically add forms to the page.

Run Linux/MQSC commands from mq client

linux ibm websphere-mq mq

Ok, I want to check if I can run some OS or MQSC commands in MQ server remotely. As long as I know, this could be done with SYSTEM.ADMIN.SVRCONN. In order to do that, I add a remote Queue Manager t...

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.