1

I'm trying to parse xml into a hierachy of objects and I'm not sure how to recurse the hierachy properly. I do have a rough solution (not a good one) which is the call to GetChild which parses an XElement and returns a collection. I'm hoping someone knows how to achieve this in a pure linq expression ie. to populate Parent-Child-Item relationship into a List without a call inline to functions like GetChild()

Thanks

var element = XElement.Parse(@"<Root RegisterVersion='1.0' xmlns='http://www.test.com.au/docs/schemas' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.test.com/docs/schemas/spin/surcharge http://www.test.com/docs/schemas/test.xsd'>
                            <Parent id='1' name='parent1'>
                                <Child id='1' name='child1'>
                                    <Item id='1' name='someitem'></Item>
                                </Child>
                            </Parent>
                            <Parent id='2' name='parent2'>
                                <Child id='2' name='child2'>
                                    <Item id='2' name='someotheritem'></Item>
                                </Child>
                            </Parent>
                            </Root>
                            ");

XNamespace ns = element.Name.Namespace;

var list =
 from compileItem in element.Elements (ns + "Parent") 
 select new Parent
 {
    Id = compileItem.Attribute("id").Value.ToString(),
    Name = compileItem.Attribute("name").Value.ToString(),
    children = GetChild(compileItem)
            // this call here I'd like to replace with another linq select
 };

 public List<Child> GetChild(XElement frag)
 {
           //etc 
 }
 public List<Item> GetItem(XElement frag)
 { 
        //etc
 }

1 Answer 1

1

Little console app.Honestly, it's much more readable with functions.

public class Parent
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public List<Child> Childrens { get; set; }
    }

    public class Child
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public List<Item> Items { get; set; }
    }
    public class Item
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    internal class Program
    {
        private class ADSetupInformation
        {
            public static void Main()
            {

                var element =
                    XElement.Parse(
                        @"<Root RegisterVersion='1.0' xmlns='http://www.test.com.au/docs/schemas' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.test.com/docs/schemas/spin/surcharge http://www.test.com/docs/schemas/test.xsd'>
                            <Parent id='1' name='parent1'>
                                <Child id='1' name='child1'>
                                    <Item id='1' name='someitem'></Item>
                                </Child>
                            </Parent>
                            <Parent id='2' name='parent2'>
                                <Child id='2' name='child2'>
                                    <Item id='2' name='someotheritem'></Item>
                                </Child>
                            </Parent>
                            </Root>
                            ");

                XNamespace ns = element.Name.Namespace;


var list =
                    element.Elements(ns + "Parent")
                        .Select(compileItem => new Parent
                                                   {
                                                       Id = Convert.ToInt32(compileItem.Attribute("id").Value),
                                                       Name = compileItem.Attribute("name").Value,
                                                       Childrens = compileItem.Elements(ns + "Child")
                                                           .Select(child => new Child
                                                                                {
                                                                                    Id = Convert.ToInt32(child.Attribute("id").Value),
                                                                                    Name = child.Attribute("name").Value,
                                                                                    Items = child.Elements(ns + "Item")
                                                                                        .Select(xe => new Item()
                                                                                                          {
                                                                                                              Id = Convert.ToInt32(xe.Attribute("id").Value),
                                                                                                              Name = xe.Attribute("name").Value,
                                                                                                          }).ToList()
                                                                                }).ToList()                             
                                                   });
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this. I think you might be right about readability too, still its good to know how.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.