locked
Load XML into TreeView

    Question

  • I have the following XML document. How can I show the following structure in a TreeView?

    -English Learning

    --Present Tense

    ---Present Simple

    ---Present Continues

    <?xml version="1.0"?>
    <manifest id="course1">
      <structure>
        <title>English Learning</title>
        <item id="lesson1">
          <title>Present Tense</title>
          <item id="lesson1part1" resid="res1">
            <title>Present Simple</title>
          </item>
          <item id="lesson1part2" resid="res2">
            <title>Present Continues</title>
          </item>
        </item>
      </structure>
      <resources>
        <resource id="res1" href="ps.htm" />
        <resource id="res2" href="pc.htm" />
      </resources>
    </manifest>


    MJ_Developer.

    Monday, April 16, 2012 5:31 AM

Answers

  • Hi, 

    When you access xNode.Name, it actually gives you node name. Based on requirement I see you are looking InnerText

    so you can have a check like, 

     if(!string.IsNullOrEmpty(xNode.InnerText))
     {
      //Add node to treeview
       inTreeNode.Nodes.Add(new TreeNode(xNode.InnerText));
    
      //other actions
     }

    Hope this helps you...


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

    Monday, April 16, 2012 11:53 AM

All replies

  • There is a coding portion for this with manual loading of the xml nodes.

    Anyway you can try reuse the code for your purpose.

    you can view it : http://support.microsoft.com/kb/317597


    Resolving n Evolving in C# (http://jeanpaulva.com)

    Monday, April 16, 2012 5:56 AM
  • Check these Codeproject articles. They give you pretty good understanding of loading XML into treeview.

    http://www.codeproject.com/KB/tree/bindablehierarchicaltree.aspx

    http://www.codeproject.com/KB/tree/dbTree.aspx

    I hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming!

    Monday, April 16, 2012 7:08 AM
  • I changed my XML file like following:

    <?xml version="1.0"?>
    <manifest>
      <structure>
        <title>English Learning</title>
        <item>
          <title>Present Tense</title>
          <item>
            <title>Present Simple</title>
          </item>
          <item>
            <title>Present Continues</title>
          </item>
        </item>
      </structure>
    </manifest>

    Here is my Code Behind:

    private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    XmlDocument dom = new XmlDocument();
                    dom.Load(textBox1.Text);
    
                    treeView1.Nodes.Clear();
                    treeView1.Nodes.Add(new TreeNode(dom.DocumentElement.Name));
                    TreeNode tNode = new TreeNode();
                    tNode = treeView1.Nodes[0];
    
                    AddNode(dom.DocumentElement, tNode);
    
                    treeView1.ExpandAll();
                }
                catch (XmlException xmlEx)
                {
                    MessageBox.Show(xmlEx.Message);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
    
            private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
            {
                XmlNode xNode;
                TreeNode tNode;
                XmlNodeList nodeList;
                int i;
    
                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.OuterXml).Trim();
                }
            }

    Here is the result:

    -manifest

    ----structure

    --------title

    ------------English Learning

    --------item

    ------------title

    ----------------Present Tense

    ------------item

    ----------------title

    --------------------Present Simple

    ------------item

    ----------------title

    --------------------Present Continues

    I want to show the titles like follow NOT the other things:

    -----English Learning

    ---------Present Tense

    ---------------Present Simple

    ---------------Present Continues

    What should I do?


    MJ_Developer.


    Monday, April 16, 2012 7:38 AM
  • Hi, 

    When you access xNode.Name, it actually gives you node name. Based on requirement I see you are looking InnerText

    so you can have a check like, 

     if(!string.IsNullOrEmpty(xNode.InnerText))
     {
      //Add node to treeview
       inTreeNode.Nodes.Add(new TreeNode(xNode.InnerText));
    
      //other actions
     }

    Hope this helps you...


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

    Monday, April 16, 2012 11:53 AM