问题描述
大家好,
因此,这听起来可能有点奇怪,但我一直在寻找它,却找不到任何有用的信息.
我想在Visual Studio(C#)中绘制树形图或层次结构图.我有一个XML文件,其中有父级和子级节点.我会阅读xml,并希望为每个节点画一个方框或三角形.我应该能够单击该三角形,因为我想提供有关特定节点的更多信息.有人知道任何有用的链接可以指导我如何实现这一目标.
我在Visual Studio中使用了MSChart Control,它很棒,但是唯一的问题是,它没有提供绘制Tree之类的层次图的方法.
预先感谢.
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.
推荐答案
//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
这篇关于如何使用Visual Studio(C#)直观地绘制XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!