C #. Net-XML and Treeview

Source: Internet
Author: User

Recently I made a major assignment in the course-web personnel management system.

 

It involves the knowledge of XML data interaction and is now recorded as a reading note.

 

I.The first step is to bind the Treeview to the XML node data in C #. net. It accurately reads the XML data and generates a tree chart.

Below isCodeAnd explanations

(1) include Database

Using system. Web; </P> <p> using system. Web. UI; </P> <p> using system. xml; </P> <p>

(2) open an XML file

Private Static xmldocument xmldoc; </P> <p> private void openxml () <br/>{< br/> xmldoc = new xmldocument (); <br/> xmldoc. load (server. mappath ("department. XML "); // server. mappath ("") can be used to obtain the absolute path. <br/>}< br/>

 

(3) Use recursive methods to add XML nodes to Treeview.

Private void expandtree () <br/>{< br/> try <br/>{< br/> numofchild = 0; </P> <p> If (xmldoc = NULL) openxml (); </P> <p> // initialize Treeview <br/> treeview2.nodes. clear (); <br/> treeview2.nodes. add (New treenode (xmldoc. documentelement. name); </P> <p> treenode tnode = new treenode (); <br/> tnode = treeview2.nodes [0]; </P> <p> // enter the XML node in the corresponding Treeview location <br/> addnode (xmldoc. documentelement, tnode); <br/> treeview2.expandall (); </P> <p >}< br/> catch (xmlexception xmlex) <br/>{< br/> throw xmlex; <br/>}< br/> catch (exception ex) <br/>{< br/> throw ex; <br/>}</P> <p> private void addnode (xmlnode inxmlnode, treenode intreenode) // recursive traversal <br/>{< br/> xmlnode xnode; <br/> treenode tnode; <br/> xmlnodelist nodelist; <br/> int I; </P> <p> // traverse all XML nodes until it reaches the leaf node. <br/> // Add the node to the Treeview during the loop Process <br/> If (inxmlnode. haschildnodes) <br/>{< br/> nodelist = inxmlnode. childnodes; <br/> for (I = 0; I <= nodelist. count-1; I ++) <br/>{< br/> xnode = inxmlnode. childnodes [I]; <br/> intreenode. childnodes. add (New treenode (xnode. name); <br/> intreenode. value = xnode. attributes ["value"]. value; </P> <p> tnode = intreenode. childnodes [I]; <br/> addnode (xnode, tnode ); <br/>}< br/> else <br/> {<br/> // reach the leaf node <br/> intreenode. TEXT = (inxmlnode. name ). trim (); <br/> intreenode. value = inxmlnode. attributes ["value"]. value; <br/> If (Int. parse (intreenode. value)> 0) numofchild ++; // records the number of nodes. This step is required for this experiment and is recorded additionally <br/>}< br/>}

 

II.Click the Treeview node, and the label on the other panel displays the corresponding text

 

(1)

For simplicity, XML is simplified here: (department. XML)

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <sunshine Municipal Education Bureau value = "-1"> </P> <p> <Education Bureau overview value = "0"> </P> <p> </ education Bureau overview> </P> <p> <functional department value = "-1"> <br/> <office value = "1"/> <br/> <Personnel Section Value = "2"/> <br/> <basic education value = "3"/> <br/> <Financial Planning value = "4"/> <br/> <Discipline Inspection and Supervision internal Audit Department value = "5"/> <br/> <Supervisory Office value = "6"/> <br/> <education Union value = "7"/> <br/> <admissions office value = "8"/> <br/> <agricultural office value = "9"/> <br/> <value = "10"/> <br /> <Teaching and Research Section value = "11"/> <br/> </functional department> </P> <p> </sunshine Municipal Education Bureau>

 

Each time you click a Treeview node, different node attribute values (in this example, the attribute is "value") are obtained from the node ").

Then, the TXT file corresponding to the name and attribute value is stored in the same project folder. Therefore, the label text will change with the click.

 

(2) provide the node adding function. Therefore, you must create a TXT file for the corresponding text of the node.

Protected void creattxtfile (int I) <br/>{< br/> string fpath = appdomain. currentdomain. basedirectory + "// edu //" + I + ". TXT "; // obtain the path of the Directory Edu under the local project folder <br/> filestream SE1 = new filestream (fpath, filemode. create, fileaccess. write); </P> <p> streamwriter Sw = new streamwriter (SE1, system. text. encoding. default); <br/> SW. write (resdescribe. text, encoding. default); // use the default encoding <br/> SW. close (); <br/>}

 

(3) Add the response function of the clicked Node

Protected void button2_click (Object sender, eventargs e) <br/>{< br/> If (depname. text. trim () = "" | resdescribe. text. trim () = "") <br/>{< br/> response. write ("<MCE: Script Type =" text/JavaScript "> <! -- <Br/> window. alert ('fill in the complete information') <br/> // --> </MCE: SCRIPT> "); <br/>}< br/> else if (xmldoc! = NULL) <br/>{< br/> openxml (); <br/> xmlnode root = xmldoc. getelementsbytagname ("function") [0]; // Add a node after the specified node <br/> xmlelement xe1 = xmldoc. createelement (depname. text); // create a <depname. text> node <br/> xe1.setattribute ("value", (++ numofchild ). tostring (); // set the genre attribute of the node <br/> root. appendchild (xe1); // Add to <> node </P> <p> xmldoc. save (server. mappath ("department. XML "); // modify and save XML data </P> <p> creattxtfile (numofchild); <br/> expandtree (); <br/> xmldoc = NULL; <br/> depname. enabled = false; // depname, resdescribe is the interface control textbox <br/> resdescribe. enabled = false; <br/>}< br/>}

 

(4) As for how to read text data, we will not introduce it here.

At the same time, this does not solve the problem of how to implement partial page refresh. We need to use Ajax knowledge and wait until we learn it.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.