tags:

views:

38

answers:

2

I have an XML file of the following structure

<Root>
    <Child Name="First">
        <Data Name="a" val="0"/>
         <Data Name="b" val="1"/>
        <Data Name="c" val="20"/>
        <Data Name="d" val="10"/>
         <Data Name="e" val="2"/>
         <Data Name="f" val="0"/>
         <Data Name="g" val="0"/>
         <Data Name="h" val="0"/>
    </Child>
    <Child Name="Second">
        <Data Name="a" val="0"/>
         <Data Name="b" val="0"/>
         <Data Name="c" val="0"/>
         <Data Name="d" val="0"/>
         <Data Name="e" val="0"/>
         <Data Name="f" val="0"/>
         <Data Name="g" val="50"/>
        <Data Name="h" val="30"/>
    </Child>
</Root>

and a dictionary like

        Dictionary<String, Dictionary<String, String>> Dict = new Dictionary<string, Dictionary<string, string>>();

i need to add the data from the xml file to the dictonary like

        First    b  1
                c  20
                d  10
                e  2

        second   g  50
                h  30

i need to add only the data element whose "val" attribute value is not equal to zero

Now i'm using nested for loops to do this .

Is there any way to do this using XML to LINQ

+1  A: 

It's a bit long, but... what can you do?

XElement root = XElement.Parse(xml);

var dict = root.Elements("Child")
    .ToDictionary(
        child => child.Attribute("Name").Value,
        child => child.Elements("Data")
            .Where(data => data.Attribute("val").Value != "0")
            .ToDictionary(
                data => data.Attribute("Name").Value,
                data => data.Attribute("val").Value
            )
    );
Dan Tao
You should use Elements rather than Descendants, since you know that the "Child" elements are direct children of the root.
Thomas Levesque
@Thomas: Sounds good. (I actually don't know LINQ to XML too well; I just figured I could see how to solve this problem in a general LINQ sort of way, so I offered an answer.)
Dan Tao
+1  A: 

Here's a way of doing it:

string path = @"E:\tmp\test.xml";
var doc = XDocument.Load(path);
var query =
    from child in doc.Root.Elements("Child")
    select new
    {
        Name = child.Attribute("Name").Value,
        Data = child.Elements("Data")
                    .Where(data => data.Attribute("val").Value != "0")
                    .ToDictionary(
                        data => data.Attribute("Name").Value,
                        data => data.Attribute("val").Value)
    };
var dict = query.ToDictionary(child => child.Name, child => child.Data);

And here's another:

string path = @"E:\tmp\test.xml";
var doc = XDocument.Load(path);
var query =
    from child in doc.Root.Elements("Child")
    from data in child.Elements("Data")
    where data.Attribute("val").Value != "0"
    group data by child.Attribute("Name").Value;

var dict = q2.ToDictionary(
            g => g.Key,
            g => g.ToDictionary(
                    data => data.Attribute("Name").Value,
                    data => data.Attribute("val").Value));

Pick whichever you prefer ;)

Thomas Levesque