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"?>
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.