從第一次接觸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
}
}
本文配套源碼