C # query, add, modify, delete, and save an application using xmldocument

Source: Internet
Author: User

In. using dataset in net to obtain XML data and save XML data is very simple and easy to use, but there are some complicated XML structures. Using dataset for operations is not as easy as using xmldocument, therefore, this article uses xmldocument in C # to perform basic operations such as XML query, adding, modifying, deleting, and saving.

See the example below:

XML file: Books. xml
1 <? XML version = "1.0" encoding = "UTF-8"?>
2 <books>
3 <book>
4 <Name> Harry Potter </Name>
5 <price> 10 </price>
6 <memo> This is a nice book. </Memo>
7 </book>
8 <book id = "B02">
9 <Name> Romance of the Three Kingdoms </Name>
10 <price> 10 </price>
11 <memo> one of the four famous books. </Memo>
12 </book>
13 <book id = "b03">
14 <Name> region </Name>
15 <price> 6 </price>
16 <memo> is one of the four famous books. </Memo>
17 </book>
18 <book id = "b04">
19 <Name> redhouse </Name>
20 <price> 5 </price>
21 <memo> is one of the four famous books. </Memo>
22 </book>
23 </books>
Below is the C # code:
1 eusing system;
2 using system. Collections. Generic;
3 using system. text;
4 using system. xml;
5
6 namespace testxml
7 {
8 class Program
9 {
10 static void main (string [] ARGs)
11 {
12 xmlelement thebook = NULL, theelem = NULL, root = NULL;
13 xmldocument xmldoc = new xmldocument ();
14 try
15 {
16 xmldoc. Load ("books. xml ");
17 root = xmldoc. documentelement;
18
19 // --- create a new book ----
20 thebook = xmldoc. createelement ("book ");
21 theelem = xmldoc. createelement ("name ");
22 theelem. innertext = "new book ";
23 thebook. appendchild (theelem );
24
25 theelem = xmldoc. createelement ("price ");
26 theelem. innertext = "20 ";
27 thebook. appendchild (theelem );
28
29 theelem = xmldoc. createelement ("memo ");
30 theelem. innertext = "better book. ";
31 thebook. appendchild (theelem );
32 root. appendchild (thebook );
33 console. Out. writeline ("--- create a new book ----");
34 console. Out. writeline (root. outerxml );
35 // --- create a new book ----
36
37 // --- make some changes to Harry Potter. ----
38 // --- query for Harry Potter ----
39 thebook = (xmlelement) root. selectsinglenode ("/books/book [name = ''harry Potter''] ");
40 console. Out. writeline ("--- find Harry Potter ----");
41 console. Out. writeline (thebook. outerxml );
42 // --- modify the price of this book at this time -----
43 thebook. getelementsbytagname ("price"). Item (0). innertext = "15"; // getelementsbytagname returns nodelist, so keep up with item (0)
44 console. Out. writeline ("--- modify the price of this book at this time ----");
45 console. Out. writeline (thebook. outerxml );
46 // --- you also want to add an attribute ID with the value B01 ----
47 thebook. setattribute ("ID", "B01 ");
48 console. Out. writeline ("--- you also want to add an attribute ID with the value B01 ----");
49 console. Out. writeline (thebook. outerxml );
50 // --- modify Harry Potter. ----
51
52 // --- delete all books whose prices are less than 10 ----
53 thebook = (xmlelement) root. selectsinglenode ("/books/book [@ ID = ''b02'']");
54 console. Out. writeline ("--- use the ID attribute to delete the book" Romance of Three Kingdoms ----");
55 console. Out. writeline (thebook. outerxml );
56 thebook. parentnode. removechild (thebook );
57 console. Out. writeline ("--- XML after deletion ----");
58 console. Out. writeline (xmldoc. outerxml );
59
60 // --- delete all books whose prices are less than 10 ----
61 xmlnodelist somebooks = root. selectnodes ("/books/book [price <10]");
62 console. Out. writeline ("--- delete all books with prices lower than 10 ---");
63 console. Out. writeline ("--- eligible books include" somebooks. Count. ---");
64
65 for (INT I = 0; I <somebooks. Count; I)
66 {
67 somebooks. Item (I). parentnode. removechild (somebooks. Item (I ));
68}
69 console. Out. writeline ("--- XML after deletion ----");
70 console. Out. writeline (xmldoc. outerxml );
71
72 xmldoc. Save ("books. xml"); // save it to books. xml
73
74 console. In. Read ();
75}
76 catch (exception E)
77 {
78 console. Out. writeline (E. Message );
79}
80}
81}
82}
The above C # code has comments, which are easy to understand.

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.