TreeView保存及读取

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);
   }
  }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值