Desolation Desolation - 3 months ago 15
C# Question

Creating a Treeview structure in c# with main nodes and subnodes

I want to display a treeview structure from information I get from a parsed xml document. It has to display only the "ItemName" and depending on the "Level" of the node is where its suppose to be like level 1 is the parent and level 2 is the child and level 3 is the childs child(leaf).

I am getting errors on when i want to add the subnodes or when i iterate through the xml nodes it only saves the last node and subnode.

this is what i've done so far

Please note, this must display on a windows form structureview

private void TreeNodeStrcutureManually(XmlDocument structure)
{


treeView1.Nodes.Clear();

XmlElement root = structure.DocumentElement;
XmlNodeList itemName = root.GetElementsByTagName("ItemName");
XmlNodeList level = root.GetElementsByTagName("Level");


string parent = "";
string childNode1 = "";
string childNode2 = "";


{
for (int i = 0; i < itemName.Count; i++)
{



TreeNode tNode;


if (level.Item(i).InnerText == "1")
{

parent = itemName.Item(i).InnerText;
tNode = treeView1.Nodes.Add(parent);

}

if (level.Item(i).InnerText == "2")
{
childNode1 = itemName.Item(i).InnerText;
tNode.Nodes.Add(childNode1);
}

if (level.Item(1).InnerText == "3")
{

childNode2 = itemName.Item(i).InnerText;
tNode.Nodes.Add(childNode2);
}
}
}


Here is an example of how the xml is returned:

<HR>
<ItemName> Addidas</ItemName>
<level> 1 </level>
</HR>


<HR>
<ItemName> Shoe </ItemName>
<level> 2 </level>
</HR>

<HR>
<ItemName> Tshirt </ItemName>
<level> 2 </level>
</HR>

<HR>
<ItemName> shoelace </ItemName>
<level> 2 </level>
</HR>


<HR>
<ItemName> Running </ItemName>
<level> 3 </level>
</HR>

<HR>
<ItemName> Nike </ItemName>
<level> 1 </level>
</HR>

Answer

this is an example how to use treeview nodes in order to create Hierarchic structure. you must have TreeNode object for each branch. as you can see in the example there must be "binding" between parent and child. hope it helps.

    private void TreeNodeStrcutureManually()
    {
TreeNode level1=new TreeNode();
TreeNode level2=new TreeNode();
TreeNode level3=new TreeNode();
TreeNode level4=new TreeNode();
TreeNode level5=new TreeNode(); 


            for (int i = 0; i <= 4; i++)
            {
               switch (i)

                {
                case 0:
                        level1 = treeView1.Nodes.Add("LEVEL1");
                        break;
                case 1:
                        level2 = level1.Nodes.Add("LEVEL2");
                    break;
                case 2:
                    level3 = level2.Nodes.Add("LEVEL3");
                    break;
                case 3:
                    level4 = level3.Nodes.Add("LEVEL4");
                    break;
                case 4:
                    level5 = level4.Nodes.Add("LEVEL5");
                    break;
                default:

                    break;
                }
            }
    }

UPDATE:

the way you are parsing xml is not correct, thats why you get:

Object reference not set to an instance of an object.

this is more efficient and strict way to do it:

    private void TreeNodeStrcutureManually(XmlDocument x)
    {

    TreeNode level1 = new TreeNode();
    TreeNode level2 = new TreeNode();
    TreeNode level3 = new TreeNode();

    XmlNodeList nl = x.SelectNodes("DOC");
    XmlNode root = nl[0];

    foreach (XmlNode xnode in root.ChildNodes)
    {
        string level = xnode.LastChild.InnerText;

        switch (level)
        {
            case " 1 ":
                level1 = treeView1.Nodes.Add(level);
                break;
            case " 2 ":
                level2 = level1.Nodes.Add(level);
                break;
            case " 3 ":
                level3 = level2.Nodes.Add(level);
                break;
            default:

                break;

        }
    }
}

and thats the xml with <DOC> </DOC> parent tag:

also note that the values of level1 node has white space (" 1 " insted of "1").

- <DOC>
- <HR>
  <ItemName>Addidas</ItemName> 
  <level>1</level> 
  </HR>
- <HR>
  <ItemName>Shoe</ItemName> 
  <level>2</level> 
  </HR>
- <HR>
  <ItemName>Tshirt</ItemName> 
  <level>2</level> 
  </HR>
- <HR>
  <ItemName>shoelace</ItemName> 
  <level>2</level> 
  </HR>
- <HR>
  <ItemName>Running</ItemName> 
  <level>3</level> 
  </HR>
- <HR>
  <ItemName>Nike</ItemName> 
  <level>1</level> 
  </HR>
  </DOC>
Comments