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:
|
Xmldocument xmldoc = new xmldocument (); Xmldoc. Load ("Bookstore. xml "); Xmlnode root = xmldoc. selectsinglenode ("Bookstore"); // query <bookstore> Xmlelement xe1 = xmldoc. createelement ("book"); // create a <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> 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> node 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 ".
|
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. |
// ==============================
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.
|
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 ");
// ==================================
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.
|
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. } } |
Reprinted from: http://blog.csdn.net/s98/archive/2004/10/17/140307.aspx
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load ("C:/AAA. xml ");
Xmlnode xn = xmldoc. selectsinglenode ("todate ");
Xmlnodelist xnl = xn. childnodes;
Foreach (xmlnode xnf in xnl)
{
Xmlelement Xe = (xmlelement) xnf;
Xmlnodelist xnf1 = Xe. childnodes;
Response. Write (xnf1 [0]. innertext); // displays the child node text.
}
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load ("C:/AAA. xml ");
Xmlnode xn = xmldoc. selectsinglenode ("todate ");
Xmlnodelist xnl = xn. childnodes;
Foreach (xmlnode xnf in xnl)
{
Xmlelement Xe = (xmlelement) xnf;
Xmlnodelist xnf1 = Xe. childnodes;
Xnf1 [0]. innertext = "10 ";
Xmldoc. Save ("C:/AAA. xml ");
Turn: http://dev.csdn.net/author/kingjiang/cd9a3c8136214b0a82b078ce16be3b25.html