Search code examples
c#xmlparsingsubtree

Parsing XML subtrees


I am fairly new to C# and I am really struggling with this problem.

So basically I have a XML file that is like this:

<groups>
<group id="1" name="group1" location="null">
    <member id="1" name="Jack" age="32"/>
    <member id="2" name="Tom" age="25"/>
    <member id="3" name="John" age="32"/>
</group>
<group id="2" name="group2" location="null">
        <member id="1" name="Bob" age="31"/>
        <member id="2" name="Michael" age="34"/>
        <member id="3" name="Mike" age="44"/>
</group>

It's not exactly that, but it follows the exact same format. So what I want to do is, put all members of a group into it's own list. So each group would have it's own list. So Jack, Tom, John (members of group id 1) would be in list1, and Bob, Michael and Mike (members of group id 2) would be in list2. I currently have this, this basically puts all members, regardless of their group, into the same list, which is not what I want. The solution is probably very simple, I just have no experience with working with XML files so no idea how to advance with this one.

String XMLURL = "link_to_xml";

using (XmlTextReader reader = new XmlTextReader(XMLURL))
{
    while (reader.Read())
    {
        switch (reader.NodeType)
        {
            case XmlNodeType.Element:
                switch (reader.Name)
                {
                    case "member":
                        if (reader.HasAttributes)
                        {
                            reader.MoveToAttribute(1);
                            players.Add(reader.Value.ToUpper());
                        }
                        break;
                }
                break;
            }
        }
    }
}

Solution

  • You can do it easily with XML Linq.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    using System.Globalization;
    
    
    namespace ConsoleApplication53
    {
        class Program
        {
            static void Main(string[] args)
            {
                string xml =
                 "<groups>" +
                    "<group id=\"1\" name=\"group1\" location=\"null\">" +
                        "<member id=\"1\" name=\"Jack\" age=\"32\"/>" +
                        "<member id=\"2\" name=\"Tom\" age=\"25\"/>" +
                        "<member id=\"3\" name=\"John\" age=\"32\"/>" +
                    "</group>" +
                    "<group id=\"2\" name=\"group2\" location=\"null\">" +
                            "<member id=\"1\" name=\"Bob\" age=\"31\"/>" +
                            "<member id=\"2\" name=\"Michael\" age=\"34\"/>" +
                            "<member id=\"3\" name=\"Mike\" age=\"44\"/>" +
                    "</group>" +
                 "</groups>";
    
                XDocument doc = XDocument.Parse(xml);
                var results = doc.Descendants("group").Select(x => new {
                    id = (int)x.Attribute("id"),
                    name = x.Attribute("name").Value,
                    location = x.Attribute("location").Value,
                    memebers = x.Elements("member").Select(y => new {
                        id = (int)y.Attribute("id"),
                        name = y.Attribute("name").Value,
                        age = (int)y.Attribute("age")
                    }).ToList()
                }).ToList();
            }
    
        }
    }