Click here to Skip to main content
14,880,624 members
Articles / Mobile Apps / Windows Mobile
Posted 22 Jun 2004


53 bookmarked

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
The article includes a sample project showing how to save the nodes of a TreeView into an XML file.

Sample Image - treeviewexport.jpg


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 + ">");
        if (ctn.GetNodeCount(true) > 0) 

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

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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


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

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
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
MemberAnonymous15-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
MemberAnonymous24-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.