從第一次接觸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
}
}