Click here to Skip to main content
Licence 
First Posted 22 Jun 2004
Views 109,709
Bookmarked 45 times

Save a TreeView into an XML File

By | 22 Jun 2004 | Article
The article includes a sample project showing how to save the nodes of a TreeView into an XML file.

Sample Image - treeviewexport.jpg

Introduction

It is sometimes useful to export a TreeView object. You can use this to populate the TreeView next time you start the application and so on. This article will show how to implement this feature.

Please note that I am not using the System.Xml namespace because the file is built dynamically during the used recursive function, and because of speed.

How to do

First, we read the root element of the TreeView in our method exportToXml(). Using the root element, we call the method parseNode(). This is a recursive function which parses every node.

private static void parseNode(TreeNode tn) 
{
    IEnumerator ie = tn.Nodes.GetEnumerator();

    string parentnode = "";

    parentnode = tn.Text;

    while (ie.MoveNext()) 
            {
        TreeNode ctn = (TreeNode) ie.Current;

        if (ctn.GetNodeCount(true) == 0) 
        {
            sr.Write(ctn.Text);
        } 
        else 
        {
            sr.Write("<" + ctn.Text + ">");
        }
        if (ctn.GetNodeCount(true) > 0) 
        {
            parseNode(ctn);
        }
    }

    sr.Write("</" + parentnode + ">");
    sr.WriteLine("");
}

For receiving all nodes, we use enumerators. The enumerators contain the nodes and we iterate through to receive the names and the values. Please note that the variable sr is a StreamReader object which I use to write the XML data into a file. This variable is defined outside of this method.

How to use the code

Have a look at the sample-code to see how to use the class TreeViewToXml.

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

About the Author

Norbert Eder

Team Leader

Austria Austria

Member

Follow on Twitter Follow on Twitter
Norbert Eder currently works as a .NET software architect at UPPER Network. He writes books and articles about WPF, Silverlight and Windows Phone 7.
 
He shares his knowledge using his weblog.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionDoes this work for elements with attributes? Pinmembertanya foster7:31 21 Apr '06  
GeneralPlz Replay Pinmemberdr_hema9:42 20 Apr '06  
JokeThank you! great code PinmemberThanhDang15:26 19 Apr '06  
GeneralXML outcome PinmemberMike Fowler9:07 6 Jul '05  
GeneralRe: XML outcome PinmemberNorbert Eder0:38 19 Aug '05  
GeneralOnly works if a single root node PinsussAnonymous14:02 15 Jun '05  
GeneralRe: Only works if a single root node PinmemberNorbert Eder0:02 17 Jun '05  
GeneralRe: Only works if a single root node Pinmembernacaroglu13:45 28 Jun '06  
GeneralRe: Only works if a single root node PinmemberFaxedHead15:25 13 Apr '07  
GeneralRe: Only works if a single root node PinmemberMember 230644919:34 26 Apr '09  
GeneralAnd now... Pinmemberleppie9:22 23 Jun '04  
GeneralRe: And now... PinmemberNorbert Eder9:59 23 Jun '04  
GeneralRe: And now... Pinmemberleppie10:20 23 Jun '04  
GeneralRe: And now... PinmemberNorbert Eder10:50 23 Jun '04  
GeneralRe: And now... Pinmembereggie510:38 23 Jun '04  
GeneralRe: And now... recursive function is quick?? PinsussAnonymous7:42 24 Jun '04  
GeneralRe: And now... recursive function is quick?? PinmemberNorbert Eder8:48 24 Jun '04  
GeneralRe: And now... Pinmemberpeterchen4:14 20 Apr '07  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 23 Jun 2004
Article Copyright 2004 by Norbert Eder
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid