Introduction
The XmlTreeview
control adds a range of useful features to the standard TreeView
for working with XML.
Background
The application I'm currently developing uses several TreeView
controls in a manner similar to the 'Solution Explorer' and 'Class View' panels in Visual Studio. To implement this functionality, I needed a way to load and save XML to and from the TreeView
as well as be able to store and retrieve data about each node in a simple and generic manner. To keep my sanity, I also needed to minimize the hassle of configuring the TreeView
to work with each XmlDocument
's data schema. Lastly, I needed events raised for each TreeNode
added and saved, so I could easily add specific business logic later on down the track.
Specifically I needed the TreeView
control to be able to...
- Load XML documents by supplying a file path.
- Load XML documents by supplying an
XmlDocument
object.
- Use
XmlNode.Name
as the TreeNode.Text
source.
- Use XmlNode.InnerText as the TreeNode.Text source.
- Use a specific
XmlNode.XmlAttribute
as the TreeNode.Text
source.
- Specify a default
ImageIndex
for each TreeNode
.
- Specify a default
SelectedImageIndex
for each TreeNode
.
- Specify a
TreeNode.ImageIndex
for each XmlNode.Name
in the source XmlDocument
.
- Specify a
TreeNode.SelectedImageIndex
for each XmlNode.name
in the source XmlDocument
.
- Customize each
TreeNode
being added to the TreeView
by intercepting the OnAddNode
event.
- Set and retrieve the value of the
XmlElement.InnerText
stored in the Tag
property of a specific TreeNode
.
- Set and retrieve the value of a specific
XmlElement.XmlAttribute
stored in the Tag
property of a specific TreeNode
.
- Set and retrieve the whole
XmlElement
stored in the Tag
property of a specific TreeNode
.
- Retrieve an
XmlDocument
representation of the TreeView
.
- Save the
XmlDocument
representation of the TreeView
to a specified file path.
- Customize each
XmlNode
being added to the XmlDocument
by intercepting the OnSaveNode
event.
You can find examples on how to use the control in the accompanying download for this article. There is a demo application and some Visual Studio 'CodeComment' documentation that will hopefully point you in the right direction. Below are a couple of examples to get you started. The examples assume you have placed an XmlTreeview
control on your form called xtv
.
Loading and saving an XmlDocument
to and from disk:
if (!xtv.LoadXml("source.xml"))
{
throw(this.xtv.Error);
}
Configure which ImageIndex
to use for each XmlNode.Name
encountered during the loading of the XmlDocument
.
this.xtv.TreeNodeImages.Add("Root", 0);
this.xtv.TreeNodeImages.Add("sqlGroup", 1);
this.xtv.TreeNodeImages.Add("sqlServer", 2);
Specify the source of the TreeNode.Text
property:
this.xtv.TreeNodeTextSource =
twentynine0677.TreeNodeTextSources.XmlNodeName;
this.xtv.TreeNodeTextSource =
twentynine0677.TreeNodeTextSources.XmlNodeInnerText;
this.xtv.TreeNodeTextSource =
twentynine0677.TreeNodeTextSources.XmlNodeAttribute;
this.xtv.XmlAttributeName = "Name";
Conclusion
This CodeProject article template that I'm writing in is asking me if I 'did anything particularly clever or wild or zany' whilst writing the code. As much as I'd like to think that I did, this was really just an exercise to broaden my knowledge of the .NET framework, and after reading some of the excellent articles on this site, I still have ways to go before you'll see me coming up with anything 'particularly clever'! :)
I hope that one of you, some of you or many of you find this helpful in your project(s) and thank you for taking the time to read this article. Special thanks to all the people who have sat down and written all those other articles before me, it was an enormous help. And thanks to CodeProject for providing a service that allows the developer community to thrive. :)
All the best,
John
History
Updates will be here.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.