The XML file operations in C # are similar to the XML operations in other languages.

Source: Internet
Author: User
  1. Xmldocument xmldoc = new xmldocument ();
  2. Xmldoc. Load ("Bookstore. xml ");
  3. // Xmldoc. loadxml ("<! -- L version = \ "1.0 \" encoding = \ "gb2312 \ --> <bookstore> </bookstore> ");
  4. Xmlnode root = xmldoc. selectsinglenode ("Bookstore"); // query <bookstore> </bookstore>
  5. Xmlelement xe1 = xmldoc. createelement ("book"); // create a <book> </book> node
  6. Xe1.setattribute ("genre", "lizan red"); // you can specify the genre attribute of the node.
  7. Xe1.setattribute ("ISBN", "2-3631-4"); // you can specify the ISBN attribute of the node.
  8. Xmlelement xesub1 = xmldoc. createelement ("title ");
  9. Xesub1.innertext = "Cs from entry to entry"; // set a text node
  10. Xe1.appendchild (xesub1); // Add it to the <book> </book> node
  11. Xmlelement xesub2 = xmldoc. createelement ("author ");
  12. Xesub2.innertext = "Hou Jie ";
  13. Xe1.appendchild (xesub2 );
  14. Xmlelement xesub3 = xmldoc. createelement ("price ");
  15. Xesub3.innertext = "58.3 ";
  16. Xe1.appendchild (xesub3 );
  17. Root. appendchild (xe1); // Add it to the <bookstore> </bookstore> node.
  18. 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 ?
  1. Xmldocument xmldoc = new xmldocument ();
  2. Xmldoc. Load ("Bookstore. xml ");
  3. // Xmldoc. loadxml ("<! -- L version = \ "1.0 \" encoding = \ "gb2312 \ --> <bookstore> </bookstore> ");
  4. Xmlnode root = xmldoc. selectsinglenode ("Bookstore"); // query <bookstore> </bookstore>
  5. Xmlelement xe1 = xmldoc. createelement ("book"); // create a <book> </book> node
  6. Xe1.setattribute ("genre", "lizan red"); // you can specify the genre attribute of the node.
  7. Xe1.setattribute ("ISBN", "2-3631-4"); // you can specify the ISBN attribute of the node.
  8. Xmlelement xesub1 = xmldoc. createelement ("title ");
  9. Xesub1.innertext = "Cs from entry to entry"; // set a text node
  10. Xe1.appendchild (xesub1); // Add it to the <book> </book> node
  11. Xmlelement xesub2 = xmldoc. createelement ("author ");
  12. Xesub2.innertext = "Hou Jie ";
  13. Xe1.appendchild (xesub2 );
  14. Xmlelement xesub3 = xmldoc. createelement ("price ");
  15. Xesub3.innertext = "58.3 ";
  16. Xe1.appendchild (xesub3 );
  17. Root. appendchild (xe1); // Add it to the <bookstore> </bookstore> node.
  18. 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 ?
  1. Xmlnodelist nodelist = xmldoc. selectsinglenode ("Bookstore"). childnodes; // obtain all the subnodes of the bookstore Node
  2. Foreach (xmlnode Xn in nodelist) // traverses all subnodes
  3. {
  4. Xmlelement Xe = (xmlelement) xn; // converts the subnode type to the xmlelement type
  5. If (Xe. getattribute ("genre") = "") // If the genre attribute value is ""
  6. {
  7. Xe. setattribute ("genre", "Update lizan red"); // you can change this attribute to "Update lizan red"
  8. Xmlnodelist NLS = Xe. childnodes; // continue to obtain all the child nodes of the Xe subnode
  9. Foreach (xmlnode xn1 in NLS) // traverse
  10. {
  11. Xmlelement xe2 = (xmlelement) xn1; // Conversion Type
  12. If (xe2.name = "author") // If you find
  13. {
  14. Xe2.innertext = "Yasheng"; // modify
  15. Break; // find and exit.
  16. }
  17. }
  18. Break;
  19. }
  20. }
  21. 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 ?
  1. Xmlnodelist xnl = xmldoc. selectsinglenode ("Bookstore"). childnodes;
  2. Foreach (xmlnode Xn in xnl)
  3. {
  4. Xmlelement Xe = (xmlelement) xn;
  5. If (Xe. getattribute ("genre") = "Fantasy ")
  6. {
  7. Xe. removeattribute ("genre"); // Delete genre attributes
  8. }
  9. Else if (Xe. getattribute ("genre") = "Update lizanhong ")
  10. {
  11. Xe. removeall (); // delete all content of the node
  12. }
  13. }
  14. 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 ?
  1. Xmlnode xn = xmldoc. selectsinglenode ("Bookstore ");
  2. Xmlnodelist xnl = xn. childnodes;
  3. Foreach (xmlnode xnf in xnl)
  4. {
  5. Xmlelement Xe = (xmlelement) xnf;
  6. Console. writeline (Xe. getattribute ("genre"); // display attribute values
  7. Console. writeline (Xe. getattribute ("ISBN "));
  8. Xmlnodelist xnf1 = Xe. childnodes;
  9. Foreach (xmlnode xn2 in xnf1)
  10. {
  11. Console. writeline (xn2.innertext); // displays the child node text.
  12. }
  13. }

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.