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 into the <bookstore> node:
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load("bookstore.xml");
XmlNode root=xmlDoc.SelectSingleNode("bookstore");//Find <bookstore>
XmlElement xe1=xmlDoc.CreateElement("book");//Create a <book> node
xe1.SetAttribute("genre","Li Zanhong");//Set the node's genre attribute
xe1.SetAttribute("ISBN","2-3631-4");//Set the node's ISBN attribute
XmlElement xesub1=xmlDoc.CreateElement("title");
xesub1.InnerText="CS from beginner to proficient";//Set text nodes
xe1.AppendChild(xesub1);//Add to <book> node
XmlElement xesub2=xmlDoc.CreateElement("author");
xesub2.InnerText="houjie";
xe1.AppendChild(xesub2);
XmlElement xesub3=xmlDoc.CreateElement("price");
xesub3.InnerText="58.3";
xe1.AppendChild(xesub3);
root.AppendChild(xe1);//Add to <bookstore> node
xmlDoc.Save("bookstore.xml");
//================================================
The 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="李zanhong" ISBN="2-3631-4">
<title>CS from entry to master</title>
<author>Houjie</author>
<price>58.3</price>
</book>
</bookstore>
2. Modify the node: Change the genre value of the node whose genre attribute value is "Li Zanhong" to "update Li Zanhong", and modify the text of the child node <author> of this node to "Ya Sheng".
XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//Get all child nodes of the bookstore node
foreach(XmlNode xn in nodeList)//Traverse all child nodes
{
XmlElement xe=(XmlElement)xn;//Convert the sub-node type to XmlElement type
if(xe.GetAttribute("genre")=="Li Zanhong")//If the genre attribute value is "Li Zanhong"
{
xe.SetAttribute("genre","update Li Zanhong");//Modify this attribute to "update Li Zanhong"
XmlNodeList nls=xe.ChildNodes;//Continue to obtain all child nodes of the xe child node
foreach(XmlNode xn1 in nls)//Traverse
{
XmlElement xe2=(XmlElement)xn1;//Conversion type
if(xe2.Name=="author")//If found
{
xe2.InnerText="Yasheng";//Modify
break;//Just find it 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李zanhong" ISBN="2-3631-4">
<title>CS from entry to master</title>
<author>Assault</author>
<price>58.3</price>
</book>
</bookstore>
3. Delete the genre attribute of the <book genre="fantasy" ISBN="2-3631-4"> node and delete the <book genre="update Li Zanhong" 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 the genre attribute
}
else if(xe.GetAttribute("genre")=="updateLi Zanhong")
{
xe.RemoveAll();//Delete all contents of this 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 value
Console.WriteLine(xe.GetAttribute("ISBN"));
XmlNodeList xnf1=xe.ChildNodes;
foreach(XmlNode xn2 in xnf1)
{
Console.WriteLine(xn2.InnerText);//Display child node text
}
}