C # Read and Write XML files
It is known that there is an XML file (bookstore. XML) as follows:
Code <? XML version = "1.0" encoding = "gb2312"?> <Bookstore> <book genre = "Fantasy" ISBN = "2-3631-4"> <title> Oberon's legacy </title> <author> corets, eva </author> <price> 5.95 </price> </book> </bookstore> 1. Insert a <book> node to the <bookstore> node:
Codexmldocument xmldoc = new xmldocument (); xmldoc. load ("Bookstore. XML "); xmlnode root = xmldoc. selectsinglenode ("Bookstore"); // find <bookstore> xmlelement xe1 = xmldoc. createelement ("book"); // create a <book> node xe1.setattribute ("genre", ""); // set the genre attribute xe1.setattribute ("ISBN ", "2-3631-4"); // set the ISBN attribute of the node xmlelement xesub1 = xmldoc. createelement ("title"); xesub1.innertext = "Cs from getting started to proficient"; // set the text node xe1.appendchild (xesub1 ); // Add the xmlelement xesub2 = xmldoc to the <book> node. createelement ("author"); xesub2.innertext = ""; xe1.appendchild (xesub2); xmlelement xesub3 = xmldoc. createelement ("price"); xesub3.innertext = "58.3"; xe1.appendchild (xesub3); root. appendchild (xe1); // Add it to the <bookstore> node xmldoc. save ("Bookstore. XML "); the result is:
Code <? XML version = "1.0" encoding = "gb2312"?> <Bookstore> <book genre = "Fantasy" ISBN = "2-3631-4"> <title> Oberon's legacy </title> <author> corets, eva </author> <price> 5.95 </price> </book> <book genre = "Li zanhong" ISBN = "2-3631-4"> <title> Cs from beginner: </title> <author> Hou Jie </author> <price> 58.3 </price> </book> </bookstore> 2. Modify nodes: change the genre value of the node whose genre attribute value is "Li zanxiang" to "Update Li zanxiang", and change the text of the child node <author> of the node to "Yasheng ".
Codexmlnodelist nodelist = xmldoc. selectsinglenode ("Bookstore "). childnodes; // obtain all subnodes of the bookstore node foreach (xmlnode Xn in nodelist) // traverse all subnodes {xmlelement Xe = (xmlelement) xn; // convert the subnode type to the xmlelement type if (Xe. getattribute ("genre") = "") // If the genre attribute value is "" {Xe. setattribute ("genre", "Update lizan red"); // modify this attribute to "Update lizan red" xmlnodelist NLS = Xe. childnodes; // continue to obtain the foreach (xmlnode xn1 in NLS) of all the child nodes of Xe. // traverse {xmlelement xe2 = (Xmlelement) xn1; // conversion type if (xe2.name = "author") // modify the break if {xe2.innertext = "Yasheng" is found; // find and exit.} break;} xmldoc. save ("Bookstore. XML "); // save. The final result is:
Code <? XML version = "1.0" encoding = "gb2312"?> <Bookstore> <book genre = "Fantasy" ISBN = "2-3631-4"> <title> Oberon's legacy </title> <author> corets, eva </author> <price> 5.95 </price> </book> <book genre = "Update Li zanhong" ISBN = "2-3631-4"> <title> CS from entry to mastery </title> <author> Yasheng </author> <price> 58.3 </price> </book> </bookstore> 3. Delete <book genre =" fantasy "ISBN =" 2-3631-4 "> genre attribute of a node, delete the <book genre = "Update lizan red" ISBN = "2-3631-4"> node.
Codexmlnodelist xnl = xmldoc. selectsinglenode ("Bookstore "). childnodes; foreach (xmlnode Xn in xnl) {xmlelement Xe = (xmlelement) xn; If (Xe. getattribute ("genre") = "Fantasy") {Xe. removeattribute ("genre"); // Delete genre attribute} else if (Xe. getattribute ("genre") = "Update lizan red") {Xe. removeall (); // delete all content of the node} xmldoc. save ("Bookstore. XML "); the final result is:
Code <? XML version = "1.0" encoding = "gb2312"?> <Bookstore> <book ISBN = "2-3631-4"> <title> Oberon's legacy </title> <author> corets, eva </author> <price> 5.95 </price> </book> </bookstore> 4. All data is displayed.
Codexmlnode xn = xmldoc. selectsinglenode ("Bookstore"); xmlnodelist xnl = xn. childnodes; foreach (xmlnode xnf in xnl) {xmlelement Xe = (xmlelement) xnf; console. writeline (Xe. getattribute ("genre"); // display the property value console. writeline (Xe. getattribute ("ISBN"); xmlnodelist xnf1 = Xe. childnodes; foreach (xmlnode xn2 in xnf1) {console. writeline (xn2.innertext); // displays the child node text }}