C # operating XML (non-LINQ Version)

Source: Internet
Author: User

<? XML version = "1.0" encoding = "UTF-8"?>
<Books>
<Book>
<Name> Harry Potter </Name>
<Price> 10 </price>
<Memo> This is a nice book. </Memo>
</Book>
<Book id = "B02">
<Name> Romance of the Three Kingdoms </Name>
<Price> 10 </price>
<Memo> one of the four famous books. </Memo>
</Book>
<Book id = "b03">
<Name> region </Name>
<Price> 6 </price>
<Memo> one of the four famous books. </Memo>
</Book>
<Book id = "b04">
<Name> redhouse </Name>
<Price> 5 </price>
<Memo> one of the four famous books. </Memo>
</Book>
</Books>

 

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. xml;

 

Namespace xmltest
{
Class Program
{
Static void main (string [] ARGs)
{
Xmlelement thebook = NULL, theelem = NULL, root = NULL;
Xmldocument xmldoc = new xmldocument ();
Try
{
String url = appdomain. currentdomain. basedirectory + "XML/books. xml ";
Xmldoc. Load ("http://www.cnblogs.com/XML/Books.xml ");
Root = xmldoc. documentelement;

// --- Create a new book ----
Thebook = xmldoc. createelement ("book ");
Theelem = xmldoc. createelement ("name ");
Theelem. innertext = "new book ";
Thebook. appendchild (theelem );

Theelem = xmldoc. createelement ("price ");
Theelem. innertext = "20 ";
Thebook. appendchild (theelem );

Theelem = xmldoc. createelement ("memo ");
Theelem. innertext = "better book. ";
Thebook. appendchild (theelem );
Root. appendchild (thebook );
Console. Out. writeline ("--- create a new book ----");
Console. Out. writeline (root. outerxml );
// --- Create a new book ----

// --- Make some changes to Harry Potter. ----
// --- Query for Harry Potter ----
Thebook = (xmlelement) root. selectsinglenode ("/books/book [name = 'Harry Potter ']");
Console. Out. writeline ("--- find Harry Potter ----");
Console. Out. writeline (thebook. outerxml );
// --- Modify the price of this book at this time -----
Thebook. getelementsbytagname ("price"). Item (0). innertext = "15"; // getelementsbytagname returns nodelist, so keep up with item (0 ). In addition, getelementsbytagname ("price") is equivalent to selectnodes (". // price ").
Console. Out. writeline ("--- modify the price of this book at this time ----");
Console. Out. writeline (thebook. outerxml );
// --- You also want to add an attribute ID with the value B01 ----
Thebook. setattribute ("ID", "B01 ");
Console. Out. writeline ("--- you also want to add an attribute ID with the value B01 ----");
Console. Out. writeline (thebook. outerxml );
// --- Modify Harry Potter. ----

// --- Delete all books whose prices are less than 10 ----
Thebook = (xmlelement) root. selectsinglenode ("/books/book [@ ID = 'b02']");
Console. Out. writeline ("--- use the ID attribute to delete the book" Romance of Three Kingdoms ----");
Console. Out. writeline (thebook. outerxml );
Thebook. parentnode. removechild (thebook );
Console. Out. writeline ("--- XML after deletion ----");
Console. Out. writeline (xmldoc. outerxml );

// --- Delete all books whose prices are less than 10 ----
Xmlnodelist somebooks = root. selectnodes ("/books/book [price <10]");
Console. Out. writeline ("--- delete all books with prices lower than 10 ---");
Console. Out. writeline ("--- eligible books include" + somebooks. Count +. ---");

For (INT I = 0; I <somebooks. Count; I ++)
{
Somebooks. Item (I). parentnode. removechild (somebooks. Item (I ));
}
Console. Out. writeline ("--- XML after deletion ----");
Console. Out. writeline (xmldoc. outerxml );

Xmldoc. Save ("books. xml"); // save it to books. xml

Console. In. Read ();
Console. Readline ();
}
Catch (exception E)
{
Console. Out. writeline (E. Message );
}
}
}
}

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.