DCSIMG
Using LINQ to XML with the Composite Pattern - David Sackstein's Blog

Using LINQ to XML with the Composite Pattern

In this post, I will show how I used LINQ to XML to construct data that complies with the Composite Pattern recursively from an XML file. See the previous post for the type definitions.

In the next post, I will put all the pieces together in a Window, and demonstrate the databinding implementation.

Here is an XML snippet with typical data for our example:

<?xml version="1.0" encoding="utf-8" ?>

<Manager Name="Big Boss">

  <Salary>50000</Salary>

  <Role>CEO</Role>

  <Department Name="Finance">

    <Budget>1000</Budget>

    <Manager Name="Alice">

      <Salary>30000</Salary>

      <Role>CFO</Role>

      <Employee Name="Bill">

        <Salary>20000</Salary>

        <Role>Accountant</Role>

      </Employee>

      <Employee Name="Sarah">

        <Salary>25000</Salary>

        <Role>Clerk</Role>

      </Employee>

    </Manager>

  </Department>

  <Department Name="R&amp;D">

    <Budget>3000</Budget>

    <Manager Name="David">

      <Salary>31000</Salary>

      <Role>VP R&amp;D</Role>

      <Employee Name="Jonathan">

      ...

This is the code:

namespace OrganizationData

{

    public class Organization

    {

        #region Load Helpers

 

        private BindingList<Composite> GetChildren(XElement node)

        {

            var q =

                from child in node.Elements()

                where child.HasElements

                select GetComposite(child);

            return new BindingList<Composite>(q.ToList());

        }

 

        private Composite GetComposite(XElement node)

        {

            switch (node.Name.LocalName)

            {

                case "Department":

                    return new Department

                    {

                        Name = (string)node.Attribute("Name"),

                        Budget = double.Parse((string)node.Element("Budget")),

                        Children = GetChildren(node)

                    };

                case "Employee":

                    return new Employee

                    {

                        Name = (string)node.Attribute("Name"),

                        Salary = Int32.Parse((string)node.Element("Salary")),

                        Role = (string)node.Element("Role"),

                        Children = GetChildren(node)

                    };

                case "Manager":

                    return new Manager

                    {

                        Name = (string)node.Attribute("Name"),

                        Salary = Int32.Parse((string)node.Element("Salary")),

                        Role = (string)node.Element("Role"),

                        Children = GetChildren(node)

                    };

            }

            return null;

        }

 

        #endregion

 

        #region Public

 

        public Composite Root { get; set; }

        public Organization()

        {

            XDocument doc = XDocument.Load("Data/Organization.xml");

            Root = GetComposite(doc.Root);

        }

 

        #endregion

    }

}

Summary

LINQ to XML makes it very easy to read XML in a strongly typed, structured way. In this post, I demonstrated use of LINQ to XML with recursion in order to read hierarchical data from file.

In the next post, I will put all the pieces together in a Window, and test the databinding implementation.

Published Friday, June 05, 2009 10:21 PM by David Sackstein

Comments

# Demonstrating DataBinding with TreeView

Saturday, June 06, 2009 2:12 AM by David Sackstein's Blog

In the previous posts I have shown how I used the Composite Pattern to describe hierarchical data , to

# re: Using LINQ to XML with the Composite Pattern

Sunday, June 07, 2009 5:16 PM by Shimon Dahan

Hi David,

Where can I download the source code?

# re: Using LINQ to XML with the Composite Pattern

Sunday, June 07, 2009 11:13 PM by David Sackstein

Hi Shimon,

You will find the complete source code for the project in the next post titled "Demonstrating Databinding with TreeView".

Here is the link, anyway.

blogs.microsoft.co.il/.../TreeViewDemo.zip

Sorry it wasnt clear.

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above:
Powered by Community Server (Commercial Edition), by Telligent Systems