Question

I am writing a simple task stopwatch application to be used in our EBS system.

I am attempting to serialize the task list into xml and load it the same way. Here is an example of an exported XML Task List:

<?xml version="1.0" encoding="utf-8"?>
<Task Task_Name="MainTask" Original_Estimated_Time="10:10:00" Current_Estimated_Time="10:10:00" Elapsed_Time="00:00:00">
    <Task Task_Name="Task 2" Original_Estimated_Time="05:00:00" Current_Estimated_Time="05:00:00" Elapsed_Time="00:00:00" />
    <Task Task_Name="Task 3" Original_Estimated_Time="15:00:00" Current_Estimated_Time="15:00:00" Elapsed_Time="00:00:00" />
</Task>

When I go to import it, I create special TreeNodes called TaskNodes to hold the information in memory. However the child nodes are not being filled with the correct title string (as you can see from this screen shot):

Here is the code being used for all of the XML loading and I cannot see the difference between what is happening to the root node and the child nodes:

    private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog dlg = new OpenFileDialog();
        dlg.Title = "Open XML Document";
        dlg.Filter = "XML Files (*.xml)|*.xml";
        dlg.FileName = Application.StartupPath + "\\..\\..\\example.xml";
        if (dlg.ShowDialog() == DialogResult.OK)
        {
            try
            {
                //Just a good practice -- change the cursor to a 
                //wait cursor while the nodes populate
                this.Cursor = Cursors.WaitCursor;
                //First, we'll load the Xml document
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(dlg.FileName);
                //Now, clear out the treeview, 
                //and add the first (root) node
                treeView1.Nodes.Clear();
                treeView1.Nodes.Add(new TaskNode(xDoc.DocumentElement.Attributes[0].Value + "", TimeSpan.Parse(xDoc.DocumentElement.Attributes[1].Value + ""), TimeSpan.Parse(xDoc.DocumentElement.Attributes[2].Value + ""), TimeSpan.Parse(xDoc.DocumentElement.Attributes[3].Value + "")));
                TaskNode tNode = (TaskNode)treeView1.Nodes[0];
                //We make a call to addTreeNode, 
                //where we'll add all of our nodes
                addTaskNode(xDoc.DocumentElement, tNode);
                //Expand the treeview to show all nodes
                treeView1.ExpandAll();
            }
            catch (XmlException xExc)
            //Exception is thrown is there is an error in the Xml
            {
                MessageBox.Show(xExc.Message);
            }
            catch (Exception ex) //General exception
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                this.Cursor = Cursors.Default; //Change the cursor back
            }
        }
    }

    //This function is called recursively until all nodes are loaded
    private void addTaskNode(XmlNode xmlNode, TaskNode taskNode)
    {
        XmlNode xNode;
        TaskNode tNode;
        XmlNodeList xNodeList;
        if (xmlNode.HasChildNodes) //The current node has children
        {
            xNodeList = xmlNode.ChildNodes;
            for (int x = 0; x <= xNodeList.Count - 1; x++)
            //Loop through the child nodes
            {
                xNode = xmlNode.ChildNodes[x];
                taskNode.Nodes.Add(new TaskNode(xNode.Attributes[0].Value + "", TimeSpan.Parse(xNode.Attributes[1].Value + ""), TimeSpan.Parse(xNode.Attributes[2].Value + ""), TimeSpan.Parse(xNode.Attributes[3].Value + "")));

                tNode = (TaskNode)taskNode.Nodes[x];
                addTaskNode(xNode, tNode);
            }
        }
        else //No children, so add the outer xml (trimming off whitespace)
            taskNode.Text = xmlNode.OuterXml.Trim();
    }

enter image description here

Was it helpful?

Solution

   else //No children, so add the outer xml (trimming off whitespace)
        //taskNode.Text = xmlNode.OuterXml.Trim();
        taskNode.Text = xmlNode.InnerXml.Trim();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top