![]() |
Desktop Development »
Tree Controls »
General
Beginner
License: The Code Project Open License (CPOL)
XML Introspection and TreeViewBy zebulon75018Xml Introspection, TreeView node and PropertyGrid |
C#
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
I wrote this article to show how to associate data of TreeView and PropertyGrid.
I have described this on my blog but in French. (That's the reason why my English in not good.)
You get data from XML (XmlDocument with XmlNodeCollection), and you use a TreeView (TreeView with TreeNode) to display them.
When the user clicks on the TreeView, you want to display the XmlNode object in propertyGrid.
Then you have to associate the XmlNode with TreeNode, to display them for a propertyGrid, like that:
propertyGrid1.SelectedObject = yourObject;
The first and bad solution is to use Index or Name of TreeNode to find in XmlDocument, the XmlNode used to build the TreeNode.
So, you can try, but you'll be crazy, for example associate XmlNode and TreeNode in a List, or Array... something strange like that.
My solution might not be better, but I like it.
Derive the TreeNode, to an XmlNodeTree associated TreeNode and XmlNode.
public class XmlNodeTree : TreeNode
{
private XmlNode mNode;
public XmlNode Node
{
get { return mNode; }
}
public XmlNodeTree(XmlNode node)
{
mNode = node;
if (node.NodeType == XmlNodeType.Text)
{
Text = node.InnerText;
}
else
{
Text = node.Name;
}
if (node.Attributes != null)
{
foreach (XmlAttribute a in node.Attributes)
{
Text += " " + a.OuterXml;
}
}
}
}
And for filling the TreeView, it's really easy:
private void FillTreeView(TreeNodeCollection c,XmlNodeList l)
{
if (l == null)
{
return;
}
foreach (XmlElement e in l)
{
XmlNodeTree n = new XmlNodeTree(e);
c.Add(n);
FillTreeView(n.Nodes, e.ChildNodes);
}
}
Call this method by:
XmlNodeTree root = new XmlNodeTree(doc.LastChild);
treeView1.Nodes.Add(root);
FillTreeView(root.Nodes,doc.LastChild.ChildNodes);
Then when the user clicks on the TreeView:
XmlNodeTree currentNode = (XmlNodeTree) e.Node;
propertyGrid1.SelectedObject = currentNode.Node;
So in one hundred lines of code, you have an XML viewer.
You can modify it easily to make a real editor. I add a text widget to view the XML content.
I tried to use FireBall editor to see XML in FireBall.CodeEditor with some problem, so I deliver the project to you without it.
Please see other stuff here.
I will publish an XML introspector later to make this more complete.
Thanks to acton101 for helping to resolve a bug.
There's a problem in FillTreeView.
Replace foreach (XmlElement e in l) with foreach (XmlNode e in l).
This is because XmlText can't be cast to XmlElement.
Read the hierarchy of XmlText.
Added another test about the existence of XmlAttribute.
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 26 Jan 2009 Editor: Deeksha Shenoy |
Copyright 2008 by zebulon75018 Everything else Copyright © CodeProject, 1999-2009 Web17 | Advertise on the Code Project |