Click here to Skip to main content
15,894,106 members
Articles / Programming Languages / C#
Article

twentynine0677 XmlTreeview

Rate me:
Please Sign up or sign in to vote.
4.20/5 (8 votes)
7 Jun 20043 min read 60.2K   609   31   5
The XmlTreeView adds a range of useful features to the standard TreeView for working with XML.

Sample Image - XmlTreeview.jpg

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:

C#
// Attempt to load an xml document into the XmlTreeview.
    if (!xtv.LoadXml("source.xml"))
    {
        // If there was an error, the Error property of the XmlTreeview
        // will be populated. You could throw this to the user or handle
        // it some other way.
        throw(this.xtv.Error);
    }    

Configure which ImageIndex to use for each XmlNode.Name encountered during the loading of the XmlDocument.

C#
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:

C#
//    Use the XmlNode.Name property for the TreeNode.Text property.
    this.xtv.TreeNodeTextSource = 
        twentynine0677.TreeNodeTextSources.XmlNodeName;
//
//    Use the XmlNode.innerText property for the TreeNode.Text property.
    this.xtv.TreeNodeTextSource = 
        twentynine0677.TreeNodeTextSources.XmlNodeInnerText;

//    Use a specific attribute in the XmlNode.Attributes
//    collection for the TreeNode.Text property.
    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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to bind TreeNode and XmlElement? Pin
Jian123456710-Aug-07 8:47
Jian123456710-Aug-07 8:47 
Hi, John

Do you have a solution to bind xmlElelment in dom and TreeNode in trewview?
Thanks,

Jian
AnswerRe: How to bind TreeNode and XmlElement? Pin
John Whiteman16-Aug-07 6:06
John Whiteman16-Aug-07 6:06 
GeneralUpdate Pin
djspirit6-Apr-05 22:49
djspirit6-Apr-05 22:49 
GeneralRe: Update Pin
John Whiteman9-Jul-05 15:55
John Whiteman9-Jul-05 15:55 
GeneralThank You Pin
massimo.magris12-Mar-05 21:22
massimo.magris12-Mar-05 21:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.