Example: Use js scripts to add, modify, and delete xml nodes

Source: Internet
Author: User


Bkjia.com xml documentUse js scripts to add, modify, and delete xml nodes. It is known that there is an XML file (bookstore. xml) as follows:

Reference content is as follows:
<? Xml version = "1.0" encoding = "gb2312"?>
<Bookstore>
<Book genre = "fantasy" ISBN = "2-3631-4">
<Title> Oberons Legacy </title>
<Author> Corets, Eva </author>
<Price> 5.95 </price>
</Book>
</Bookstore>


1. Insert a <book> node to the <bookstore> node:

Reference content is as follows:
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", "author"); // you can specify the genre attribute of a 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 = "author ";
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:
Reference content is as follows:
<? Xml version = "1.0" encoding = "gb2312"?>
<Bookstore>
<Book genre = "fantasy" ISBN = "2-3631-4">
<Title> Oberons Legacy </title>
<Author> Corets, Eva </author>
<Price> 5.95 </price>
</Book>
<Book genre = "author" ISBN = "2-3631-4">
<Title> CS from entry to entry </title>
<Author> author </author>
<Price> 58.3 </price>
</Book>
</Bookstore>


2. Modify the node: Change the genre value of the node whose genre attribute value is "author" to "update author", and change the text of the child node <author> of the node to "Yasheng ".

Reference content is as follows:
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") = "author") // if the genre attribute value is "author"
{
Xe. SetAttribute ("genre", "update Author"); // modify the attribute to "update Author"

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:
Reference content is as follows:
<? Xml version = "1.0" encoding = "gb2312"?>
<Bookstore>
<Book genre = "fantasy" ISBN = "2-3631-4">
<Title> Oberons Legacy </title>
<Author> Corets, Eva </author>
<Price> 5.95 </price>
</Book>
<Book genre = "update Author" ISBN = "2-3631-4">
<Title> CSS from entry to entry </title>
<Author> Yasheng </author>
<Price> 58.3 </price>
</Book>
</Bookstore>

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.