Writing TreeView Contents To XML

  • Thread starter Thread starter Robert Gale
  • Start date Start date
R

Robert Gale

I am writing an RSS reader in C# and I'm trying to write the contents
of a TreeView to an XML file. I have tried using XmlWriter and have
managed to write simple tree structures but I get confused when it
comes to dealing with complex tree structures such as those that have
multiple folders and sub folders.

I have tried searching Code Project and the Net but haven't had much
joy in finding a solution. I have only been using C# for a few months
and my programming knowledge is fairly limited.

Is XmlWriter the best way to write out a TreeView? How would you go
about going through each of the folders and writing the feeds within
them?

If there is anything else that I need to include let me know.
 
Robert said:
I am writing an RSS reader in C# and I'm trying to write the contents
of a TreeView to an XML file. I have tried using XmlWriter and have
managed to write simple tree structures but I get confused when it
comes to dealing with complex tree structures such as those that have
multiple folders and sub folders.

Is XmlWriter the best way to write out a TreeView? How would you go
about going through each of the folders and writing the feeds within
them?

you might want to start with learning how to traverse a tree:

http://www.brpreiss.com/books/opus5/html/page259.html
 
Thanks for the quick reply. I am familiar with ways of traversing a tree
I'm just not familiar with how to write code to do so using XmlWriter.
 
Hi ,

Nothing less here is a more closer answer...

I just took it from one of my old code, carefully go through this I can even
give the full code to you.. it has both write and read facility but for a
customized treenode object...

//Starting here.......
// Call the procedure using the TreeView.
private void CallRecursive()
{
str = File.CreateText("My.xml");
myData = "";
// browse each node recursively.
TreeNodeCollection nodes = treeView1.Nodes;
foreach (TreeNode n in nodes)
{
BrowseRecursive(n, ref myData);
}
str.Write(myData);
str.Close();
}



private void BrowseRecursive(TreeNode treeNode, ref string myStr)
{
// browse each node recursively.

if (treeNode.Nodes.Count >0)
myStr += "<" + treeNode.Text + ">";
else
myStr += treeNode.Text;
foreach (TreeNode tn in treeNode.Nodes)
{
BrowseRecursive(tn, ref myStr);
}
if (treeNode.Nodes.Count >0)
myStr += "</" + treeNode.Text + ">";
}

Regards,
Nirosh.
 
Great! This could be what I was looking for. I'll give this a try this
morning and let you know how I get on.
 
Thanks Nirosh. Your code was a great help. With a little tweaking I was
able to change it for my needs.
 
Back
Top