Populate C# treeview using XML source data

I have the following XML:

<cases>
 <case name="active_type1_smith_1" office="newcastle" />
 <case name="active_type1_jones_1" office="sunderland" />
 <case name="active_type2_elliott_1" office="newcastle" />
 <case name="active_type2_davies_1" office="london" />
</cases>

Open in new window


and am using the following to iterate through a list of the cases:

XDocument.Load("case.xml");

foreach(XElement Case In (XDocument.Load("case.xml").Descendants().Where(node => node.Name == "case")
{
 ...
}

Open in new window


What I am looking for is a way to populate a C# based treeview object based on the XElements obtained. The office attribute should be the parent nodes within the tree and then a child node based on the name attrbiutes 'type' e.g. type1 and then the full name itself. If a parent of child node already exists the current node should be added to the existing node structure. So the above would look like...

newcastle
  type1
     active_type1_smith_1
  type2
    active_type2_elliott_1
sunderland
  type1
     active_type1_jones_1
london
  type2
     active_type2_davies_1

Open in new window


I would like to implement this as part of a C# solution and am a bit of a newbie. Also I would like the ability to control the apperance and event handlers of the individual tree nodes - which I think I can work out myself but would like the solution to give me some access to the tree nodes being added to facilitate this.
Blowfelt82Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Easwaran ParamasivamCommented:
By calling recursive method this is possible. Please refer sample snippet below.


           
 XmlDataDocument xmldoc = new XmlDataDocument();

            XmlNode xmlnode ;

            FileStream fs = new FileStream("case.xml", FileMode.Open, FileAccess.Read);

            xmldoc.Load(fs);

            xmlnode = xmldoc.ChildNodes[1];

            treeView1.Nodes.Clear();

            treeView1.Nodes.Add(new TreeNode(xmldoc.DocumentElement.Name));

            TreeNode tNode ;

            tNode = treeView1.Nodes[0];

            AddNode(xmlnode, tNode);

        }



        private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)

        {

            XmlNode xNode ;

            TreeNode tNode ;

            XmlNodeList nodeList ;

            int i = 0;

            if (inXmlNode.HasChildNodes)

            {

                nodeList = inXmlNode.ChildNodes;

                for (i = 0; i <= nodeList.Count - 1; i++)

                {

                    xNode = inXmlNode.ChildNodes[i];

                    inTreeNode.Nodes.Add(new TreeNode(xNode.Name));

                    tNode = inTreeNode.Nodes[i];

                    AddNode(xNode, tNode);

                }

            }

            else

            {

                inTreeNode.Text = inXmlNode.InnerText.ToString();

            }

        }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.