Click here to Skip to main content
14,972,762 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I used the following code found on code project for loading nodes from my Treeview to an xml file, and then again from the xml to the TreeView on my windows form application. when the nodes from my treeview are loaded to the xml file, all the children of the root node are being concatenated, which results in displaying all the children nodes as one single child node of the root node. Any help would be greatly appreciated.

Here is the methods:

C#
public void SaveTree(TreeView tv, string FileName)
        {
            xmlWriter = new XmlTextWriter(FileName, System.Text.Encoding.UTF8);

            xmlWriter.WriteStartDocument();

            //writes the root node to xml document
            xmlWriter.WriteStartElement(this.treeView1.Nodes[0].Text);

            foreach (TreeNode node in tv.Nodes)
            {
                SaveNode(node.Nodes);
            }

            xmlWriter.Flush();
            xmlWriter.Close();
        }

        public void SaveNode(TreeNodeCollection tnc)
        {
            foreach (TreeNode tNode in tnc)
            {
                if (tNode.Nodes.Count > 0)
                {
                    foreach (TreeNode tNode2 in tNode.Nodes)
                    {
                        xmlWriter.WriteString(tNode2.Text);
                        
                        SaveNode(tNode.Nodes);
                    }
                }
                else
                {
                    xmlWriter.WriteString(tNode.Text);
                    
                }
            }
        }

        public void PopulateTreeView()
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;

                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(FileName);

                this.treeView1.Nodes.Clear();
                this.treeView1.Nodes.Add(new TreeNode(xDoc.DocumentElement.Name));


                TreeNode tNode = new TreeNode();
                tNode = (TreeNode)this.treeView1.Nodes[0];

                AddTreeNode(xDoc.DocumentElement, tNode);

                this.Cursor = Cursors.Default;
            }
            catch (XmlException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void AddTreeNode(XmlNode xmlNode, TreeNode treeNode)
        {
            XmlNode XMLNode;
            TreeNode Tnode;
            XmlNodeList NodeList;

            if (xmlNode.HasChildNodes)
            {
                NodeList = xmlNode.ChildNodes;

                for (int i = 0; i <= NodeList.Count - 1; i++)
                {
                    XMLNode = xmlNode.ChildNodes[i];
                    treeNode.Nodes.Add(new TreeNode(XMLNode.Name));
                    Tnode = treeNode.Nodes[i];
                    AddTreeNode(XMLNode, Tnode);
                }
            }
            else
            {
                treeNode.Text = xmlNode.OuterXml.Trim();
            }
        }


Here is the AddNode method:

C#
public void AddNode()
{
if (this.treeView1.SelectedNode != null)
{
NewNode newNode = new NewNode();
newNode.ShowDialog();

TreeNode tNode = new TreeNode();
tNode.Name = newNode.nName.ToString();
tNode.Text = newNode.nText.ToString();

this.treeView1.SelectedNode.Nodes.Add(tNode);
}
else
{

NewNode neNode = new NewNode();
neNode.ShowDialog();

TreeNode tnode = new TreeNode();
tnode.Name = neNode.nName.ToString();
tnode.Text = neNode.nText.ToString();

this.treeView1.Nodes.Add(tnode);
}

this.treeView1.ExpandAll();
}


if, for instance, the first node to be added is "hahaha", and on to that, I add three child nodes a, b, c, everything works fine when viewing in debug mode, but when the data is written to the xml file, the root node is seen as a single node, but if the root node's children have no children, they are concatenated! This might be as I made the text OuterXml if the children have no children. The xmlnode has no Text property, only a Name property, and when choosing the Name property, it only returns #text. So my question is how do I retrieve the text property of the xmlNode?
Posted
Updated 4-Jul-11 13:21pm
v3

I can't find the AddNode method, but I can see that in your AddTreeNode method, you add it as well as recursing. Have you stepped through the code to see what's happening ? Are you saying your XML has multiple levels but they all get put on at the same level ? Sounds like you need a breakpoints in your AddTreeNode method to see what is going on. Also, making the text OuterXml, means making a single node equal to all the XML from there down. You want the text of just that node, surely ?
   
Comments
Andrew797 4-Jul-11 17:55pm
   
Here is the AddNode method:

public void AddNode()
{
if (this.treeView1.SelectedNode != null)
{
NewNode newNode = new NewNode();
newNode.ShowDialog();

TreeNode tNode = new TreeNode();
tNode.Name = newNode.nName.ToString();
tNode.Text = newNode.nText.ToString();

this.treeView1.SelectedNode.Nodes.Add(tNode);
}
else
{

NewNode neNode = new NewNode();
neNode.ShowDialog();

TreeNode tnode = new TreeNode();
tnode.Name = neNode.nName.ToString();
tnode.Text = neNode.nText.ToString();

this.treeView1.Nodes.Add(tnode);
}

this.treeView1.ExpandAll();
}

if, for instance, the first node to be added is "hahaha", and on to that, I add three child nodes a, b, c, everything works fine, but when the data is written to the xml file, the root node is seen as a single node, but if the root node's children have no children, they are concatenated! This might be as I made the text OuterXml if the children have no children. The xmlnode has no Text property, only a Name property, and when choosing the Name property, it only returns #text
OK, I deleted your 'reply' and pasted it in to your question. First of all, XmlNode is a base class. Your node is an XmlElement, which has the properties you need. Beyond that, I'm not really following what's going wrong, does the XML reflect what you expect it to ? Is the problem in reading or writing ? If it's in writing, perhaps if you posted the XML as well as an explanation of how it's not showing what you want it to ?
   
Comments
Andrew797 5-Jul-11 3:41am
   
using the AddNode method, I have added the following four nodes to the treeView:

Year2011 //this is the root main node
a //child of Year2011
b //child of Year2011
c //child of Year2011

Currently the nodes are all in a hierarchy form, but when writing these nodes to the xml file, the output looks as follow:


<year2011>abc

when I read the nodes from the xml file back into my treeView, it shows the nodes a, b, and c, as one node with the text concatenated. i want the nodes to be written to the xml file as seperate nodes, not to be concatenated, and when the nodes are read back from xml file, I want it to be displayed in a hierarchy again
Are you sure you need low-level XML programming? The problem is already nicely solved for you.

I would suggest you use Data Contract instead. Its serializer will automatically store and restore all your data, not just tree, but an object graph of the arbitrary structure (it means, it can contain circular references, the serializer will handle it correctly). Also, the XML code is very human-readable, the schema can be world-unique (use your company site based name space for this purpose) and the technique is the most non-intrusive — you only add attributes to the data types and members (usually [DataContract] and [DataMember]).

See: http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

See also my past answers:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating a property files...[^].

—SA
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900