Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello to all,

so this may sound a little weird question but i looked for it and could not found any helpful information.

I would like to draw a Tree or Hierarchical diagram in Visual Studio (C#). I have an XML file where i have parent and child nodes. I would read the xml and for each node i would like to draw a box or triangle or so. I should be able to click on that triangle because i would like to present further information regarding a particular node. Does anybody know any useful links which can guide me how to achieve this.


I used MSChart Control in visual studio and its wonderful but the only problem is, it does not provide a way to draw Hierarchical diagrams like Tree or so.

thanks in advance.
Posted
Comments
n.podbielski 3-Oct-12 9:06am    
Are you trying to make VS add in? Or you using VS as IDE.

1 solution

Hello,

Your question seems a bit vague but interesting, I would recommend using a
treeview becuase it supports nodes and images like you said.

C#
//First lets read the xml..
List<treenode> parents = new List<treenode>();
List<treenode> nodes = new List<treenode>(); //Create a storage for xml nodes
string[] temp = new string[256]; //Helper for organizing nodes
XmlTextReader tr = new XmlTextReader(File.Open(@"C:/Users/Austin/Desktop/example.xml", FileMode.Open)); //Create Xml Reader
while (tr.Read())
{
    if (tr.NodeType == XmlNodeType.Element && !tr.HasValue)
        parents.Add(new TreeNode("<" + tr.Name + ">")); //Parent nodes
    if (tr.NodeType == XmlNodeType.Element && tr.HasValue)
        temp[tr.LineNumber] = "<" + tr.Name + ">"; //Child nodes
    if (tr.NodeType == XmlNodeType.Text)
        nodes.Add(new TreeNode((temp[tr.LineNumber] ?? "") + tr.Value));
}
tr.Close();

ImageList list = null; // Implement your images here...
treeView1.ImageList = list;

TreeNode[] collection = new TreeNode[parents.Count];
for (int i = 0; i < parents.Count; i++)
{
    collection[i] = new TreeNode(parents[i].Text, nodes.ToArray()); //Combine parents and child nodes
    collection[i].ImageIndex = 1; //Index of imagelist to show
}

treeView1.Nodes.AddRange(collection); //Add the collection to your treeview
 
Share this answer
 
v2
Comments
imran.mm7 10-Oct-12 7:14am    
Thanks for your reply, I understand the first part of your solution where you propose to read the xml file and extract the information to a treeview Control.

Second part of your solution suggests to read the collection of nodes and use the ImageList control to add specific images (at-least what i think).

but here is my problem again, hopefully this time its not vague.

I can read the xml file, I can use the treeview control and put the information from xml file into parent and child nodes and the tree is populated.

The second step is to draw this tree in visual shape. e.g., Using a drawing control. but simple drawing does not help. let me explain it further. once i get the information from xml file and populate the tree. I want to print the tree in graphical shape where i can click the individual elements of the graph (e.g. triangle or button) and when i click it i would like to open a dialogue box or something which shows further semantic information about the element. I am looking forward if there is any control which let me draw the tree in graphical shape and let me select the individual items.

thanks in advance

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



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