c add delete change and check the operation example of xml
- 2020-05-30 20:57:25
- OfStack
It is known that there is one 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,
<
bookstore
>
Insert 1 node
<
book
>
Nodes:
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load("bookstore.xml");
XmlNode root=xmlDoc.SelectSingleNode("bookstore");// To find the <bookstore>
XmlElement xe1=xmlDoc.CreateElement("book");// create 1 a <book> node
xe1.SetAttribute("genre"," Li Zangong ");// Set the node genre attribute
xe1.SetAttribute("ISBN","2-3631-4");// Set the node ISBN attribute
XmlElement xesub1=xmlDoc.CreateElement("title");
xesub1.InnerText="CS From introduction to mastery ";// Set text node
xe1.AppendChild(xesub1);// Added to the <book> A node
XmlElement xesub2=xmlDoc.CreateElement("author");
xesub2.InnerText=" All parts ";
xe1.AppendChild(xesub2);
XmlElement xesub3=xmlDoc.CreateElement("price");
xesub3.InnerText="58.3";
xe1.AppendChild(xesub3);
root.AppendChild(xe1);// Added to the <bookstore> A 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=" Li Zangong " ISBN="2-3631-4">
<title>CS From introduction to mastery </title>
<author> All parts </author>
<price>58.3</price>
</book>
</bookstore>
2. Modify the node: change the genre value of the node whose genre attribute value is "li zanghong" to "update li zanghong", and change the child node of the node
<
author
>
The text was changed to "yaseng".
XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;// To obtain bookstore All child nodes of a node
foreach(XmlNode xn in nodeList)// Traverse all child nodes
{
XmlElement xe=(XmlElement)xn;// Converts the child node type to XmlElement type
if(xe.GetAttribute("genre")==" Li Zangong ")// if genre Property value is "li zanhong"
{
xe.SetAttribute("genre","update Li Zangong ");// Then change the property to" update Li Zanhong"
XmlNodeList nls=xe.ChildNodes;// Continue to get xe All the child nodes of the child nodes
foreach(XmlNode xn1 in nls)// traverse
{
XmlElement xe2=(XmlElement)xn1;// Conversion type
if(xe2.Name=="author")// If you find
{
xe2.InnerText=" The win ";// The modified
break;// Just find the 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 Zangong " ISBN="2-3631-4">
<title>CS From introduction to mastery </title>
<author> The win </author>
<price>58.3</price>
</book>
</bookstore>
3, delete,
<
book genre="fantasy" ISBN="2-3631-4"
>
The genre property of the node, delete
<
book genre = "update li changhong" 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 attribute
}
else if(xe.GetAttribute("genre")=="update Li Zangong ")
{
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 values
Console.WriteLine(xe.GetAttribute("ISBN"));
XmlNodeList xnf1=xe.ChildNodes;
foreach(XmlNode xn2 in xnf1)
{
Console.WriteLine(xn2.InnerText);// Displays child node point text
}
}