By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,184 Members | 1,429 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,184 IT Pros & Developers. It's quick & easy.

treeview

P: n/a
What's the best way to create new nodes on a treeview 2.0 and save them to
an xml file?

Thanks,

Victor
Aug 31 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hello Victor,

As for the ASP.NET 2.0 TreeView control, its TreeNode collection is
generated through statically declaration in aspx template or databinding.
However, the databinding support only allow use to read from a given XML
file(XML datasource) and then populate the TreeView, there is no direct
interface to export the nodes reversely.

I'm thinking whether what you want to do is just save the current Nodes
(structure , collection) into a XML file so that we can load it from file
later and populate the TreeView control. Is the XML file's format
predesigned, or just any format as long as we can load it again later?
Here are my suggestion about such two cases:

1. If you just want to export the TreeNodes of a treeview control into a
certain XML file and can reload it again into Treeview, you can consider
using the XmlSerializer to serialize the TreeView.Nodes collection into a
xml file. Later, we can also use the XmlSerializer to reload the TreeNodes
from xml file and add it into TreeView e.g:

==============================
protected void btnExport_Click(object sender, EventArgs e)
{
TreeNodeCollection nodes = TreeView1.Nodes;

XmlSerializer serializer = new
XmlSerializer(typeof(TreeNodeCollection));

StreamWriter writer = new
StreamWriter(@"D:\temp\xml\TreeView1.nodes");
serializer.Serialize(writer,nodes);

writer.Close();

}
protected void btnImport_Click(object sender, EventArgs e)
{
XmlSerializer serializer = new
XmlSerializer(typeof(TreeNodeCollection));

StreamReader reader = new
StreamReader(@"D:\temp\xml\TreeView1.nodes");
TreeNodeCollection nodes = serializer.Deserialize(reader) as
TreeNodeCollection;

reader.Close();

TreeView1.Nodes.Clear();

foreach (TreeNode node in nodes)
{
TreeView1.Nodes.Add(node);
}

}
========================================

2. If the XML file's format is designed by you and need to meet certain
requirement, I'm afraid you have to create custom class which has methods
to do the following things:

** load XML file through System.Xml components and create TreeNode
collection from XmlDocument

** Create XML Document(meet our schema ) based on a give TreeNodeCollection
and save it into a file.
Please feel free to let me know if you have any other consideration or
ideas on this.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Sep 1 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.