When it comes to XML, children's shoes, which have a certain programming base, are more clear. The XML file format is similar to nested styles, and this unique hierarchy makes it easy to think of a directory tree, so it makes sense to have a way of making a connection between the two. This begs the question: How do you convert XML stored data into a directory tree form??
First, I'll post an XML code:
<nativeplace> <china text= "China" value= "China" > <shandong text= "Shandong" value= "Shandong" > < Jining text= "Jining" value= "Jining" > <renchengqu text= "Rencheng District" value= "Rencheng District" > <changgouzhen text= "Long Gou zhen" value= "Long Ditch Town" > </ChangGouZhen> </ReChengQu> </JiNing> </ShanDong> </China></nativeplace>
This code clearly shows us the hierarchical relationship between them, and then we'll link the area in this XML to the TreeView in C # to achieve the tree effect we want.
First we need to load the XML into the TreeView. Loading XML is what we call finding XML and then reading the XML content. Here I introduce a section about finding the path to the assembly, and I am accustomed to putting the XML file under assembly Bin/debug, because C # provides this access.
The next piece of code is the way to read the assembly debug path of the project:
public string Getassemblyfiledirectory () {#region variable description string FilePath, STRINGX; FileInfo fileinfox;//Here I create a class of action folder, of course you can also use other way URI Urix; #endregion #region Get the location of the. dll// Gets the location of the. dll file, primarily used to determine the database file Stringx = assembly.getcallingassembly (). codebase;//generates a URI to be used to parse Urix = new Uri (STRINGX);//Gets the directory of the configuration file Fileinfox = new FileInfo (Urix.localpath); FilePath = fileinfox.directoryname;//if (!) ( Filepath.substring (filepath.length-1, 1) = = @ "\"))//{FilePath = String.Concat (FilePath, @ "\");} return FilePath; #endregion}
so we get the location of the XML file, and what we do next is load the XML file. The specific code is as follows:
Helper = new Helper ();//This class contains a method to get the assembly directory Xmlpath = helper. Getassemblyfiledirectory () + "/resources/dictionary.xml"; XmlDocument xmldoc = new XmlDocument (); Creates an XML document class xmldoc. Load (xmlpath);//loading XML document XmlNode = xmldoc. Documentelement.selectsinglenode ("Nativeplace");//Get node nativeplace nodes = xmlnode.childnodes;// Get all the child nodes under the Nativeplace node, this child is the true child node, not all "descendants" node Bindxmltotreeview (nodes, nativeplacetreeview.nodes);// The method is to load the XML nodes into the TreeView directory tree.
The next thing we do is how to traverse all the sub-nodes under the nativeplace and the grandchildren nodes. Here's how:
public void Bindxmltotreeview (XmlNodeList nodeList, treenodecollection treeNode) { foreach (XmlNode node in NodeList) { XmlElement xe = (XmlElement) node;//Create an XML element that transforms each node obtained into an XML element for easy property assignment. TreeNode newtreenode = new TreeNode ();//Create a new TreeNode and save the information in XmlNode to TreeNode. Newtreenode.text = Xe. GetAttribute ("text"); Newtreenode.name = Xe. GetAttribute ("value"); Treenode.add (Newtreenode);//Loads the TreeNode that are created and contains information into the TreeNode collection. if (node. HasChildNodes) { Bindxmltotreeview (node. ChildNodes, newtreenode.nodes);//grandson Node loading }} }
so we've created a directory tree, and then I'll show you a directory tree that I created earlier:
Sometimes we need to get the text of TreeNode, however, how do we get the full node name when we click on a node (plus the text of all its parent nodes)? , we can actually achieve this:
private void okButton_Click (object sender, EventArgs e) { parentnode (nativeplacetreeview.selectednode);// Here is the click node that will get the pass past this . Close (); }
private void ParentNode (TreeNode text) { TreeNode fulltext = text; while (text. Parent = null) { Fulltext.text = Text. Parent.text +fulltext.text;//This always gets the node's parent Text. Text = text. parent;//Our text node is the click node that is passed over and then assigns its parent node to it, so it can be pushed up. Know the root node in the TreeView. } nativeplace["Address"] = fulltext.text;//here just copy the text of the node to the corresponding column of the DataRow passed over, we can do without the tube, Now you can use the watch to see if the Fulltext.text will show the full directory name. }
Click the OK button to display the detailed name in the DataGridView:
Here's how XML is loaded into the TreeView!
Once again I wish you a happy Lantern Festival!!
How C # Implements loading a tree directory from XML and displays the full text