從第一次接觸XML開始就想寫一個能實現tree和XML靈活轉換的類了。
寫這個類大概用去了將近半天的時間,花的時間有些長了。呵呵。。好在收穫頗多,熟練了XML的讀寫類,對C#中的foreach迴圈也有了更深的認識。
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace TreeExXML
{
class TreeExXMLCls
{
private TreeView thetreeview;
private string xmlfilepath;
XmlTextWriter textWriter;
XmlNode Xmlroot;
XmlDocument textdoc;
public TreeExXMLCls()
{
//----建構函式
textdoc = new XmlDocument();
}
~TreeExXMLCls()
{
//----解構函式
}
#region 遍曆treeview並實現向XML的轉化
/// <summary>
/// 遍曆treeview並實現向XML的轉化
/// </summary>
/// <param name="TheTreeView">樹控制項對象</param>
/// <param name="XMLFilePath">XML輸出路徑</param>
/// <returns>0表示函數順利執行</returns>
public int TreeToXML(TreeView TheTreeView, string XMLFilePath)
{
//-------初始化轉換環境變數
thetreeview = TheTreeView;
xmlfilepath = XMLFilePath;
textWriter = new XmlTextWriter(xmlfilepath, null);
//-------建立XML寫操作對象
textWriter.Formatting = Formatting.Indented;
//-------開始寫過程,調用WriteStartDocument方法
textWriter.WriteStartDocument();
//-------寫入說明
textWriter.WriteComment("this XML is created from a tree");
textWriter.WriteComment("By 思月行雲");
//-------添加第一個根節點
textWriter.WriteStartElement("TreeExXMLCls");
textWriter.WriteEndElement();
//------ 寫文檔結束,調用WriteEndDocument方法
textWriter.WriteEndDocument();
//-----關閉輸入資料流
textWriter.Close();
//-------建立XMLDocument對象
textdoc.Load(xmlfilepath);
//------選中根節點
XmlElement Xmlnode=textdoc.CreateElement(thetreeview.Nodes[0].Text);
Xmlroot = textdoc.SelectSingleNode("TreeExXMLCls");
//------遍曆原treeview控制項,並產生相應的XML
TransTreeSav(thetreeview.Nodes, (XmlElement)Xmlroot);
return 0;
}
private int TransTreeSav(TreeNodeCollection nodes, XmlElement ParXmlnode)
{
//-------遍曆樹的各個故障節點,同時添加節點至XML
XmlElement xmlnode;
Xmlroot = textdoc.SelectSingleNode("TreeExXMLCls");
foreach (TreeNode node in nodes)
{
xmlnode = textdoc.CreateElement(node.Text);
ParXmlnode.AppendChild(xmlnode);
if (node.Nodes.Count > 0)
{
TransTreeSav(node.Nodes, xmlnode);
}
}
textdoc.Save(xmlfilepath);
return 0;
}
#endregion
#region 遍曆XML並實現向tree的轉化
/// <summary>
/// 遍曆treeview並實現向XML的轉化
/// </summary>
/// <param name="XMLFilePath">XML輸出路徑</param>
/// <param name="TheTreeView">樹控制項對象</param>
/// <returns>0表示函數順利執行</returns>
public int XMLToTree(string XMLFilePath,TreeView TheTreeView)
{
//-------重新初始化轉換環境變數
thetreeview = TheTreeView;
xmlfilepath = XMLFilePath;
//-------重新對XMLDocument對象賦值
textdoc.Load(xmlfilepath);
XmlNode root = textdoc.SelectSingleNode("TreeExXMLCls");
foreach (XmlNode subXmlnod in root.ChildNodes)
{
TreeNode trerotnod = new TreeNode();
trerotnod.Text = subXmlnod.Name;
thetreeview.Nodes.Add(trerotnod);
TransXML(subXmlnod.ChildNodes, trerotnod);
}
return 0;
}
private int TransXML(XmlNodeList Xmlnodes,TreeNode partrenod)
{
//------遍曆XML中的所有節點,仿照treeview節點遍曆函數
foreach (XmlNode xmlnod in Xmlnodes)
{
TreeNode subtrnod = new TreeNode();
subtrnod.Text = xmlnod.Name;
partrenod.Nodes.Add(subtrnod);
if (xmlnod.ChildNodes.Count > 0)
{
TransXML(xmlnod.ChildNodes, subtrnod);
}
}
return 0;
}
#endregion
}
}
本文配套源碼