Click here to Skip to main content
Click here to Skip to main content

twentynine0677 XmlTreeview

, 7 Jun 2004
Rate this:
Please Sign up or sign in to vote.
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:

// 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.

    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:

//    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'! Smile | :)

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. Smile | :)

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

Share

About the Author

John Whiteman
Web Developer
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
QuestionHow to bind TreeNode and XmlElement? PinmemberJian123456710-Aug-07 9:47 
AnswerRe: How to bind TreeNode and XmlElement? PinmemberJohn Whiteman16-Aug-07 7:06 
GeneralUpdate Pinmemberdjspirit6-Apr-05 23:49 
GeneralRe: Update PinmemberJohn Whiteman9-Jul-05 16:55 
GeneralThank You Pinmembermassimo.magris12-Mar-05 22:22 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411022.1 | Last Updated 8 Jun 2004
Article Copyright 2004 by John Whiteman
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid