Click here to Skip to main content
15,868,141 members
Articles / Mobile Apps / Windows Mobile
Tip/Trick

Save a TreeView into an XML File

Rate me:
Please Sign up or sign in to vote.
2.86/5 (11 votes)
24 Jul 2013CPOL 166.8K   4.8K   53   20
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.

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


Written By
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

Comments and Discussions

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