- Xmldocument xmldoc = new xmldocument ();
- Xmldoc. Load ("Bookstore. xml ");
- // Xmldoc. loadxml ("<! -- L version = \ "1.0 \" encoding = \ "gb2312 \ --> <bookstore> </bookstore> ");
- Xmlnode root = xmldoc. selectsinglenode ("Bookstore"); // query <bookstore> </bookstore>
- Xmlelement xe1 = xmldoc. createelement ("book"); // create a <book> </book> node
- Xe1.setattribute ("genre", "lizan red"); // you can specify the genre attribute of the node.
- Xe1.setattribute ("ISBN", "2-3631-4"); // you can specify the ISBN attribute of the node.
- Xmlelement xesub1 = xmldoc. createelement ("title ");
- Xesub1.innertext = "Cs from entry to entry"; // set a text node
- Xe1.appendchild (xesub1); // Add it to the <book> </book> node
- Xmlelement xesub2 = xmldoc. createelement ("author ");
- Xesub2.innertext = "Hou Jie ";
- Xe1.appendchild (xesub2 );
- Xmlelement xesub3 = xmldoc. createelement ("price ");
- Xesub3.innertext = "58.3 ";
- Xe1.appendchild (xesub3 );
- Root. appendchild (xe1); // Add it to the <bookstore> </bookstore> node.
- Xmldoc. Save ("Bookstore. xml ");
Xmldocument xmldoc = new xmldocument (); xmldoc. Load ("Bookstore. xml"); // xmldoc. loadxml ("
"); Xmlnode root = xmldoc. selectsinglenode (" Bookstore "); // search
Xmlelement xe1 = xmldoc. createelement ("book"); // create
Node xe1.setattribute ("genre", ""); // set the node 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
Xmlelement xesub2 = xmldoc in the node. createelement ("author"); xesub2.innertext = ""; xe1.appendchild (xesub2); xmlelement xesub3 = xmldoc. createelement ("price"); xesub3.innertext = "58.3"; xe1.appendchild (xesub3); root. appendchild (xe1); // Add
Xmldoc. Save ("Bookstore. xml ");
It is known that there is an XML file (bookstore. XML) as follows:
<? 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:
View plain
Copy to clipboard
Print
?
- Xmldocument xmldoc = new xmldocument ();
- Xmldoc. Load ("Bookstore. xml ");
- // Xmldoc. loadxml ("<! -- L version = \ "1.0 \" encoding = \ "gb2312 \ --> <bookstore> </bookstore> ");
- Xmlnode root = xmldoc. selectsinglenode ("Bookstore"); // query <bookstore> </bookstore>
- Xmlelement xe1 = xmldoc. createelement ("book"); // create a <book> </book> node
- Xe1.setattribute ("genre", "lizan red"); // you can specify the genre attribute of the node.
- Xe1.setattribute ("ISBN", "2-3631-4"); // you can specify the ISBN attribute of the node.
- Xmlelement xesub1 = xmldoc. createelement ("title ");
- Xesub1.innertext = "Cs from entry to entry"; // set a text node
- Xe1.appendchild (xesub1); // Add it to the <book> </book> node
- Xmlelement xesub2 = xmldoc. createelement ("author ");
- Xesub2.innertext = "Hou Jie ";
- Xe1.appendchild (xesub2 );
- Xmlelement xesub3 = xmldoc. createelement ("price ");
- Xesub3.innertext = "58.3 ";
- Xe1.appendchild (xesub3 );
- Root. appendchild (xe1); // Add it to the <bookstore> </bookstore> node.
- Xmldoc. Save ("Bookstore. xml ");
Xmldocument xmldoc = new xmldocument (); xmldoc. Load ("Bookstore. xml"); // xmldoc. loadxml ("
"); Xmlnode root = xmldoc. selectsinglenode (" Bookstore "); // search
Xmlelement xe1 = xmldoc. createelement ("book"); // create
Node xe1.setattribute ("genre", ""); // set the node 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
Xmlelement xesub2 = xmldoc in the node. createelement ("author"); xesub2.innertext = ""; xe1.appendchild (xesub2); xmlelement xesub3 = xmldoc. createelement ("price"); xesub3.innertext = "58.3"; xe1.appendchild (xesub3); root. appendchild (xe1); // Add
Xmldoc. Save ("Bookstore. xml ");
// ================================================ ==========
Result:
<? 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 entry to entry </title>
<Author> Hou Jie </author>
<Price> 58.3 </price>
</Book>
</Bookstore>
2. Modify the node: Change the genre value of the node whose genre attribute value is "Li zenhong" to "Update Li zenhong ", modify the text of the child node <author> of the node to "Yasheng ".
View plain
Copy to clipboard
Print
?
- Xmlnodelist nodelist = xmldoc. selectsinglenode ("Bookstore"). childnodes; // obtain all the subnodes of the bookstore Node
- Foreach (xmlnode Xn in nodelist) // traverses all subnodes
- {
- Xmlelement Xe = (xmlelement) xn; // converts the subnode type to the xmlelement type
- If (Xe. getattribute ("genre") = "") // If the genre attribute value is ""
- {
- Xe. setattribute ("genre", "Update lizan red"); // you can change this attribute to "Update lizan red"
- Xmlnodelist NLS = Xe. childnodes; // continue to obtain all the child nodes of the Xe subnode
- Foreach (xmlnode xn1 in NLS) // traverse
- {
- Xmlelement xe2 = (xmlelement) xn1; // Conversion Type
- If (xe2.name = "author") // If you find
- {
- Xe2.innertext = "Yasheng"; // modify
- Break; // find and exit.
- }
- }
- Break;
- }
- }
- Xmldoc. Save ("Bookstore. xml"); // save.
Xmlnodelist 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 get all the sub-nodes of Xe sub-nodes foreach (xmlnode xn1 in NLS) // traverse {xmlele Ment xe2 = (xmlelement) xn1; // 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:
<? 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 entry </title>
<Author> Yasheng </author>
<Price> 58.3 </price>
</Book>
</Bookstore>
3. Delete the genre attribute of the <book genre = "Fantasy" ISBN = "2-3631-4"> node, delete the <book genre = "Update lizan red" ISBN = "2-3631-4"> node.
View plain
Copy to clipboard
Print
?
- Xmlnodelist xnl = xmldoc. selectsinglenode ("Bookstore"). childnodes;
- Foreach (xmlnode Xn in xnl)
- {
- Xmlelement Xe = (xmlelement) xn;
- If (Xe. getattribute ("genre") = "Fantasy ")
- {
- Xe. removeattribute ("genre"); // Delete genre attributes
- }
- Else if (Xe. getattribute ("genre") = "Update lizanhong ")
- {
- Xe. removeall (); // delete all content of the node
- }
- }
- Xmldoc. Save ("Bookstore. xml ");
Xmlnodelist 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:
<? 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>
<Book>
</Book>
</Bookstore>
4. display all data.
View plain
Copy to clipboard
Print
?
- Xmlnode xn = xmldoc. selectsinglenode ("Bookstore ");
- Xmlnodelist xnl = xn. childnodes;
- Foreach (xmlnode xnf in xnl)
- {
- Xmlelement Xe = (xmlelement) xnf;
- Console. writeline (Xe. getattribute ("genre"); // display attribute values
- Console. writeline (Xe. getattribute ("ISBN "));
- Xmlnodelist xnf1 = Xe. childnodes;
- Foreach (xmlnode xn2 in xnf1)
- {
- Console. writeline (xn2.innertext); // displays the child node text.
- }
- }