Click here to Skip to main content
13,146,402 members (49,225 online)
Click here to Skip to main content
Add your own
alternative version

Stats

138.9K views
4.3K downloads
52 bookmarked
Posted 22 Jun 2004

Save a TreeView into an XML File

, 24 Jul 2013
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Norbert Eder
Team Leader
Austria Austria
Norbert Eder currently works as a .NET software architect at UPPER Network. He writes books and articles about WPF, Silverlight and Windows Phone 7.

For further information see his website or his project site

You may also be interested in...

Comments and Discussions

 
QuestionWell... Pin
Marc Clifton24-Jul-13 4:15
protectorMarc Clifton24-Jul-13 4:15 
AnswerRe: Well... Pin
Norbert Eder24-Jul-13 20:34
memberNorbert Eder24-Jul-13 20:34 
QuestionDoes this work for elements with attributes? Pin
tanya foster21-Apr-06 7:31
membertanya foster21-Apr-06 7:31 
GeneralPlz Replay Pin
dr_hema20-Apr-06 9:42
memberdr_hema20-Apr-06 9:42 
JokeThank you! great code Pin
ThanhDang19-Apr-06 15:26
memberThanhDang19-Apr-06 15:26 
GeneralXML outcome Pin
Mike Fowler6-Jul-05 9:07
memberMike Fowler6-Jul-05 9:07 
GeneralRe: XML outcome Pin
Norbert Eder19-Aug-05 0:38
memberNorbert Eder19-Aug-05 0:38 
GeneralOnly works if a single root node Pin
Anonymous15-Jun-05 14:02
sussAnonymous15-Jun-05 14:02 
GeneralRe: Only works if a single root node Pin
Norbert Eder17-Jun-05 0:02
memberNorbert Eder17-Jun-05 0:02 
GeneralRe: Only works if a single root node Pin
nacaroglu28-Jun-06 13:45
membernacaroglu28-Jun-06 13:45 
GeneralRe: Only works if a single root node Pin
FaxedHead13-Apr-07 15:25
memberFaxedHead13-Apr-07 15:25 
GeneralRe: Only works if a single root node Pin
Member 230644926-Apr-09 19:34
memberMember 230644926-Apr-09 19:34 
GeneralAnd now... Pin
leppie23-Jun-04 9:22
memberleppie23-Jun-04 9:22 
GeneralRe: And now... Pin
Norbert Eder23-Jun-04 9:59
memberNorbert Eder23-Jun-04 9:59 
GeneralRe: And now... Pin
leppie23-Jun-04 10:20
memberleppie23-Jun-04 10:20 
GeneralRe: And now... Pin
Norbert Eder23-Jun-04 10:50
memberNorbert Eder23-Jun-04 10:50 
GeneralRe: And now... Pin
eggie523-Jun-04 10:38
membereggie523-Jun-04 10:38 
GeneralRe: And now... recursive function is quick?? Pin
Anonymous24-Jun-04 7:42
sussAnonymous24-Jun-04 7:42 
GeneralRe: And now... recursive function is quick?? Pin
Norbert Eder24-Jun-04 8:48
memberNorbert Eder24-Jun-04 8:48 
GeneralRe: And now... Pin
peterchen20-Apr-07 4:14
memberpeterchen20-Apr-07 4:14 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170915.1 | Last Updated 24 Jul 2013
Article Copyright 2004 by Norbert Eder
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid