SoFunction
Updated on 2025-03-01

C# Getting Started with XML Operation Example

It is known that there is an XML file () as follows:

 
Copy the codeThe code is as follows:

 <?xmlversion="1.0"encoding="gb2312"?>
 <bookstore>
 <bookgenre="fantasy"ISBN="2-3631-4">
 <title>Oberon'sLegacy</title>
 <author>Corets,Eva</author>
 <price>5.95</price>
 </book>
 </bookstore>
 

1. Insert a <book> node into the <bookstore> node:
 
Copy the codeThe code is as follows:

 XmlDocumentxmlDoc=newXmlDocument();
 ("");
XmlNoderoot=("bookstore");//Find <bookstore>
XmlElementxe1=xmlDoc.createElement_x("book");//Create a <book> node
("genre","Li Zanhong");//Set the genre attribute of this node
("ISBN","2-3631-4");//Set the ISBN attribute of this node

 XmlElementxesub1=xmlDoc.createElement_x("title");
="CS from Beginner to Mastery";//Set Text Node
(xesub1);//Add to the <book> node
 XmlElementxesub2=xmlDoc.createElement_x("author");
="not yet";
 (xesub2);
 XmlElementxesub3=xmlDoc.createElement_x("price");
 ="58.3";
 (xesub3);

(xe1);//Add to the <bookstore> node
 ("");

 

 //================
The result is:

 
 
Copy the codeThe code is as follows:


 <?xmlversion="1.0"encoding="gb2312"?>
 <bookstore>
 <bookgenre="fantasy"ISBN="2-3631-4">
 <title>Oberon'sLegacy</title>
 <author>Corets,Eva</author>
 <price>5.95</price>
 </book>
<bookgenre="Li Zanhong"ISBN="2-3631-4">
<title>CS From Beginner to Proficiency</title>
<author>Hou Jie</author>
 <price>58.3</price>
 </book>
 </bookstore>
 

2. Modify node: Change the genre value of the node with the genre attribute value "Li Zanhong" to "update Li Zanhong", and modify the text of the child node <author> of the node to "Yasheng".

 
Copy the codeThe code is as follows:

XmlNodeListnodeList=("bookstore").ChildNodes;//Get all children of the bookstore node
foreach(XmlNodexninnodeList)//Transfer all child nodes
 {
XmlElementxe=(XmlElement)xn;//Convert child node type to XmlElement type
if(("genre")=="Li Zanhong")//If the genre attribute value is "Li Zanhong"
 {
("genre","update Li Zanhong");//Then modify this property to "update Li Zanhong"

XmlNodeListnls=;//Continue to get all children of xe child nodes
foreach(XmlNodexn1innls)//Travel
 {
XmlElementxe2=(XmlElement)xn1;//Conversion type
if(=="author")//If found
 {
="Yasheng";//Then modify
break;//Just find and exit
 }
 }
 break;
 }
 }

("");//Save.

 

 //=================

The final result is:

 
Copy the codeThe code is as follows:

 <?xmlversion="1.0"encoding="gb2312"?>
 <bookstore>
 <bookgenre="fantasy"ISBN="2-3631-4">
 <title>Oberon'sLegacy</title>
 <author>Corets,Eva</author>
 <price>5.95</price>
 </book>
<bookgenre="updateLi Zanhong"ISBN="2-3631-4">
<title>CS From Beginner to Proficiency</title>
<author>Asia Victor</author>
 <price>58.3</price>
 </book>
 </bookstore>

 

3. Delete the genre attribute of the <bookgenre="fantasy"ISBN="2-3631-4"> node, and delete the <bookgenre="updateLi Zanhong"ISBN="2-3631-4"> node.

 
Copy the codeThe code is as follows:

 XmlNodeListxnl=("bookstore").ChildNodes;

 foreach(XmlNodexninxnl)
 {
 XmlElementxe=(XmlElement)xn;

 
 if(("genre")=="fantasy")
 {
("genre");//Delete the genre attribute
 }
elseif(("genre")=="updateLi Zanhong")
 {
();//Delete all contents of this node
 }
 }
 ("");
 

 //====================

The final result is:

 
Copy the codeThe code is as follows:

 <?xmlversion="1.0"encoding="gb2312"?>
 <bookstore>
 <bookISBN="2-3631-4">
 <title>Oberon'sLegacy</title>
 <author>Corets,Eva</author>
 <price>5.95</price>
 </book>
 <book>
 </book>
 </bookstore>
 


4. Display all data.

 
Copy the codeThe code is as follows:

 XmlNodexn=("bookstore");

 XmlNodeListxnl=;

 foreach(XmlNodexnfinxnl)
 {
 XmlElementxe=(XmlElement)xnf;
(("genre"));//Show attribute values
 (("ISBN"));

 XmlNodeListxnf1=;
 foreach(XmlNodexn2inxnf1)
 {
();//Show child node point text
 }
 }