treeview

G

Guest

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

Thanks,

Victor
 
S

Steven Cheng[MSFT]

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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.

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



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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
ScottChare

Latest Threads

Top