Saturday, March 1, 2008 STOP! This is an old post. Are you sure it's still relevant?

Save an ASP.NET TreeView Control to an XML File

Here are two methods to help you take a TreeView web control and save / export / serialize it to a file. Add these two statements to the top of the file:
using System.Xml;
using System.Text;
These two methods take the TreeView and recursively loop through all the TreeNode controls.
/// <summary>
/// Recurses through a TreeView web control exports the results
/// to an XML file nested to match the TreeView. Properties are
/// saved as attributes when a value is set.
/// <summary>
/// <param name="myTreeView">A TreeView web control.</param>
/// <param name="myFile">A file path and name with extension.</param>
protected void TreeViewToXml(System.Web.UI.WebControls.TreeView
myTreeView, string myFile)
{
try
{
XmlTextWriter xmlWriter = new XmlTextWriter(myFile, Encoding.UTF8);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.Indentation = 3;
xmlWriter.WriteStartDocument();
// Opens <TreeView>, the root node.
xmlWriter.WriteStartElement("TreeView");

// Go through child nodes.
ProcessNodes(myTreeView.Nodes, xmlWriter);

// </TreeView>, the root node.
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();

// Closes the object and saves the document to disk.
xmlWriter.Close();
}
catch
{
throw; // Throw any exceptions up the line.
}
}

/// <summary>
/// Recursively processes each TreeNode within a TreeNodeCollection
/// from a TreeView web control.
/// </summary>
/// <param name="myTreeNodes">A TreeNodeCollection from a TreeView web
/// control.</param>
/// <param name="myWriter">A XmlTextWriter being used to hold the XML
/// results from the TreeViewToXml method.</param>
protected void ProcessNodes(TreeNodeCollection myTreeNodes,
XmlTextWriter myWriter)
{
foreach (TreeNode thisNode in myTreeNodes)
{
myWriter.WriteStartElement("TreeNode"); // <TreeNode>.

// Go through each property and set it as an attribute if it exists.
if(!String.IsNullOrEmpty(thisNode.Text))
myWriter.WriteAttributeString("Text", thisNode.Text);
if (!String.IsNullOrEmpty(thisNode.ImageToolTip))
myWriter.WriteAttributeString("ImageToolTip",
thisNode.ImageToolTip);
if (!String.IsNullOrEmpty(thisNode.ImageUrl))
myWriter.WriteAttributeString("ImageUrl", thisNode.ImageUrl);
if (!String.IsNullOrEmpty(thisNode.NavigateUrl))
myWriter.WriteAttributeString("NavigateUrl",
thisNode.NavigateUrl);
if (!String.IsNullOrEmpty(thisNode.SelectAction.ToString()))
myWriter.WriteAttributeString("SelectAction",
thisNode.SelectAction.ToString());
if (!String.IsNullOrEmpty(thisNode.Target))
myWriter.WriteAttributeString("Target", thisNode.Target);
if (!String.IsNullOrEmpty(thisNode.ToolTip))
myWriter.WriteAttributeString("ToolTip", thisNode.ToolTip);
if (!String.IsNullOrEmpty(thisNode.Value))
myWriter.WriteAttributeString("Value", thisNode.Value);
if (!String.IsNullOrEmpty(thisNode.ValuePath))
myWriter.WriteAttributeString("ValuePath", thisNode.ValuePath);
if (thisNode.ShowCheckBox.HasValue)
myWriter.WriteAttributeString("ShowCheckBox",
thisNode.ShowCheckBox.ToString());
if (thisNode.Expanded.HasValue)
myWriter.WriteAttributeString("Expanded",
thisNode.Expanded.ToString());
myWriter.WriteAttributeString("Selected",
thisNode.Selected.ToString());
myWriter.WriteAttributeString("Checked",
thisNode.Checked.ToString());

// Recurse through any child nodes.
if (thisNode.ChildNodes.Count > 0)
ProcessNodes(thisNode.ChildNodes, myWriter);

myWriter.WriteEndElement(); // </TreeNode>.
}
}

No comments: