1

How to display XML data in TreeView control using Linq To XML and Also In contrast save TreeView data in XML by using Linq

example xml file

<?xml version="1.0" encoding="utf-8"?>
<factors>
  <factor number="1" price="1000">
    <code>12</code>
    <group>A</group>
  </factor>
</factors>

I'm using Windows Forms and C#

2

1 Answer 1

2

Parse your XML and depending on what XML elements you want as nodes add them:

var xmlSource = @"<?xml version=""1.0"" encoding=""utf-8""?>
                    <factors>
                        <factor number=""1"" price=""1000"">
                            <code>12</code>
                            <group>A</group>
                        </factor>
                        <factor number=""2"" price=""10"">
                            <code>1</code>
                            <group>B</group>
                        </factor>
                    </factors>";

var xml = XDocument.Parse(xmlSource);
var factors = xml.Root.Descendants("factor").ToList();
// create tree and add root node
// TreeView tr = new TreeView();
// var root = tr.Nodes.Add("Factors");
foreach (var factor in factors)
{
    var nodeNumber = factor.Attribute("number").Value;
    var subNodeCode = factor.Element("code").Value;
    // add attribute as node name
    // var node = root.Nodes.Add(nodeNumber);
    // add elements as sub nodes
    // node.Nodes.Add(subNodeCode)
}

The code for saving from the tree to XML is exactly the same, just in the oposite direction - iterate through all nodes and extract and build the xml elements and/or attributes. You should know best which element or attribute you want to show as tree node and vice versa.

Read the documentation for the TreeView class and LINQ2XML starting for example from the XDocument helper class


Assuming that your TreeView is named tree, then with the following code you can create quickly a XML document from the nodes:

var xml = new XDocument();
var rootElement = new XElement("factors");
foreach (TreeNode node in tree.Nodes)
{
    foreach (TreeNode subNode in node.Nodes)
    {
        var attribute = new XAttribute("number", subNode.Text);
        var element = new XElement("factor", attribute);
        foreach (TreeNode subSubNode in subNode.Nodes)
        {
            var subElement = new XElement("code", subSubNode.Text);
            element.Add(subElement);
        }
        rootElement.Add(element);
    }
}
xml.Add(rootElement);
xml.ToString(); // gives you the XML code
// OR save the XML directly to file
//xml.Save(@"c:\temp\output.xml");

Far better and efficient would be to write a recursive function that gives you filled XElement object back. You should also use the Tag TreeNode property to store the information for every TreeNode.

Sign up to request clarification or add additional context in comments.

2 Comments

thx @pasty but now, how can save TreeView data in XML by using Linq?
I have added sample code for creating and saving the XML from the tree nodes in my answer.

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.