An example XML file is this:
<?xml version="1.0" encoding="UTF-8"?>
<game>
<name>bomber</name>
<behaviors-used>
<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>
</behaviors-used>
</game>
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
}).Single();
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
behaviorlistis not really a list right now – you want the elements not the combined text, so instead of using theValueproperty of the parent node, you should retrieve theElementswith name “behavior”:From the resulting list you can then easily retrieve the properties: