C # XML file read and modify

Source: Internet
Author: User

C # Read and write XML files
It is known that an XML file (Bookstore.xml) is as follows:

Code
<?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:

Code
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 Zhanhong");//Set the node genre property
Xe1. SetAttribute ("ISBN", "2-3631-4");//Set the node ISBN property

XmlElement xesub1=xmldoc.createelement ("title");
Xesub1. Innertext= "CS from beginner to proficient";//Set Text node
Xe1. AppendChild (XESUB1);//Add to <book> node
XmlElement xesub2=xmldoc.createelement ("author");
Xesub2. Innertext= "Waiting for the Czech Republic";
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:

Code
<?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 Zhanhong" isbn= "2-3631-4" >
<title>cs from getting started to mastering </title>
<author> </author>
<price>58.3</price>
</book>
</bookstore>

2. Modify the node: Change the genre value of the node with the genre property value to "Li Zhanhong" to "Update Li Zhanhong", and modify the text of the node's child nodes <author> to "Ya Sheng".

Code

The final result is:

 code 
<?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.9 5</price>
</book>
<book genre= "update Li Zhanhong" isbn= "2-3631-4";
<title>cs from Getting started to mastering </ Title>,
<author> </author>,
<price>58.3</price>
</book>
</ Bookstore>

3, delete isbn= properties of <book genre= "fantasy" 2-3631-4 "Genre" > node, delete <book genre= " Update Li Zhanhong "isbn=" 2-3631-4 "> node.
Code
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 property
}
else if (XE. GetAttribute ("genre") = = "Update Li Zhanhong")
{
Xe. RemoveAll ();//delete the entire contents of the node
}
}
Xmldoc.save ("Bookstore.xml");
The final result is:
Code
<?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 property values
Console.WriteLine (XE. GetAttribute ("ISBN"));

XmlNodeList Xnf1=xe. ChildNodes;
foreach (XmlNode xn2 in Xnf1)
{
Console.WriteLine (xn2. InnerText);//Display child node point text
}
}

C # XML file read and modify

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.