Starting with the first contact with XML, you want to write a class that enables tree and XML to be flexibly transformed.
It took almost half a day to write this class, and the time spent was a little longer. Oh.. Fortunately, a lot of harvest, proficiency in XML reading and writing classes, C # in the Foreach Loop also has a deeper understanding.
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 ()
{
----constructors
Textdoc = new XmlDocument ();
}
~treeexxmlcls ()
{
----destructor
}
#region traverse the TreeView and implement the transformation to XML
<summary>
Traversing the TreeView and implementing the Transformation to XML
</summary>
<param name= "Thetreeview" > Tree control Objects </param>
<param name= "Xmlfilepath" >xml Output path </param>
<returns>0 representation function Smooth execution </returns>
public int Treetoxml (TreeView thetreeview, String xmlfilepath)
{
-------Initialization Transformation environment variable
Thetreeview = Thetreeview;
Xmlfilepath = Xmlfilepath;
TextWriter = new XmlTextWriter (Xmlfilepath, NULL);
-------Create an XML write Object
textwriter.formatting = formatting.indented;
-------start the write process, call the WriteStartDocument method
Textwriter.writestartdocument ();
-------Write Description
Textwriter.writecomment ("This XML are created from");
Textwriter.writecomment ("By Think Moon Line Cloud");
-------Add the first root node
Textwriter.writestartelement ("Treeexxmlcls");
Textwriter.writeendelement ();
------The end of the write document, call the WriteEndDocument method
Textwriter.writeenddocument ();
-----Close the input stream
Textwriter.close ();
-------Create a XmlDocument object
Textdoc. Load (Xmlfilepath);
------Select root Node
XmlElement Xmlnode=textdoc. CreateElement (Thetreeview. Nodes[0]. Text);
XmlRoot = Textdoc. selectSingleNode ("Treeexxmlcls");
------Traverse the original TreeView control and generate the corresponding XML
Transtreesav (Thetreeview. Nodes, (XmlElement) xmlroot);
return 0;
}
private int Transtreesav (treenodecollection nodes, XmlElement parxmlnode)
{
-------Traverse each failed node of the tree while adding nodes to 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 traversing XML and realizing the transformation to tree
<summary>
Traversing the TreeView and implementing the Transformation to XML
</summary>
<param name= "Xmlfilepath" >xml Output path </param>
<param name= "Thetreeview" > Tree control Objects </param>
<returns>0 representation function Smooth execution </returns>
public int Xmltotree (string Xmlfilepath,treeview thetreeview)
{
-------reinitialization of transformation environment variables
Thetreeview = Thetreeview;
Xmlfilepath = Xmlfilepath;
To assign a value to a XmlDocument object-------
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)
{
------traversing all nodes in the XML, imitating the TreeView node traversal function
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
}
}
This article supporting source code