using System;
using System.Windows.Forms;
using System.Xml;
using System.Globalization;
namespace TreeSample
{
/// <summary>
/// Summary description for TreeFunctions.
/// </summary>
public class TreeFunctions
{
public TreeFunctions()
{
//Add constructor logic here
}
internal System.Xml.XmlDocument Tree2XML(ref System.Windows.Forms.TreeView oTreeView)
{
System.Text.StringBuilder strOutput = new System.Text.StringBuilder();
strOutput.Append("<?xml version=/"1.0/"?>/n");
strOutput.Append("<hierarchy>/n");
WalkTreeNodes(oTreeView.Nodes, ref strOutput);
strOutput.Append("</hierarchy>");
System.Xml.XmlDocument oXML = new System.Xml.XmlDocument();
try
{
oXML.LoadXml(strOutput.ToString());
}
catch(System.Xml.XmlException excXML)
{
MessageBox.Show("Cannot Load XML");
Application.Exit();
}
return oXML;
}
private void WalkTreeNodes(System.Windows.Forms.TreeNodeCollection xNodeCol, ref System.Text.StringBuilder strOut)
{
foreach(EnhancedTreeNode xNode in xNodeCol)
{
strOut.Append("<node id=/"" + xNode.ItemID.ToString(CultureInfo.InvariantCulture) + "/" position=/"" + xNode.Index + "/">/n");
strOut.Append("<![CDATA[" + xNode.ItemName + "]]>/n");
if(xNode.GetNodeCount(true)>0)
{
WalkTreeNodes(xNode.Nodes, ref strOut);
}
strOut.Append("</node>/n");
}
}
internal void CreateTree(string strHierarchy, System.Windows.Forms.TreeNodeCollection xTreeNodeCol)
{
System.Xml.XmlTextReader xtr = new System.Xml.XmlTextReader(
new System.IO.StringReader(strHierarchy));
int LastDepth = -1;
int CurrentDepth = -1;
EnhancedTreeNode xNode = null;
TreeNodeCollection xCol = null;
xtr.WhitespaceHandling = WhitespaceHandling.None;
while(xtr.Read())
{
if(xtr.NodeType==XmlNodeType.Element)
{
switch(xtr.Name)
{
case "node":
xNode = new EnhancedTreeNode();
CurrentDepth = xtr.Depth;
xtr.MoveToAttribute("id");
xtr.ReadAttributeValue();
xNode.ItemID = Convert.ToInt32(xtr.Value, CultureInfo.InvariantCulture);
xtr.MoveToElement();
xtr.MoveToAttribute("inactive");
xtr.ReadAttributeValue();
if(LastDepth==-1) //it's the root!
{
xCol = xTreeNodeCol;
}
else if(CurrentDepth<LastDepth) //Move Up A Node
{
for(int i=0;i<LastDepth-CurrentDepth;i++)
{
xCol = xCol[xCol.Count-1].Parent.Parent.Nodes;
}
}
else if(CurrentDepth==LastDepth) //Stay in the same collection
{
xCol = xCol;
}
else if(CurrentDepth>LastDepth) // Move Down A Node
{
for(int i=0;i<CurrentDepth-LastDepth;i++)
{
xCol = xCol[xCol.Count-1].Nodes;
}
}
xtr.MoveToElement();
LastDepth = xtr.Depth;
break;
case "name":
xNode.ItemName = xtr.ReadString();
xCol.Add(xNode);
break;
default:
break;
}
}
}
xtr.Close();
}
}
public sealed class EnhancedTreeNode : System.Windows.Forms.TreeNode
{
private int m_intItemID = -1;
private string m_strItemName = string.Empty;
public EnhancedTreeNode()
{
//Add constructor logic here
}
public EnhancedTreeNode(string ItemName, int ItemID)
{
m_intItemID = ItemID;
m_strItemName = ItemName;
this.Text = ItemName;
}
public int ItemID
{
get{return m_intItemID;}
set{m_intItemID=value;}
}
public string ItemName
{
get{return m_strItemName;}
set{m_strItemName=value;this.Text=m_strItemName;}
}
}
}
//读取XML
private void menuItem3_Click(object sender, System.EventArgs e)
{
DialogResult answer = openFileDialog1.ShowDialog();
if(answer!=DialogResult.Cancel)
{
System.IO.TextReader tr = System.IO.File.OpenText(openFileDialog1.FileName);
TreeFunctions tf = new TreeFunctions();
string hierarchy = tr.ReadToEnd();
tr.Close();
tf.CreateTree(hierarchy, treeView1.Nodes);
}
}
//退出
private void menuItem5_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
//保存为XML文件
private void menuItem4_Click(object sender, System.EventArgs e)
{
DialogResult answer = saveFileDialog1.ShowDialog();
if(answer!=DialogResult.Cancel)
{
TreeFunctions tf = new TreeFunctions();
tf.Tree2XML(ref treeView1).Save(saveFileDialog1.FileName);
}
}