XML operations in C #

Source: Internet
Author: User

Namespace to be added:
Using System. Xml;

Define several public objects:
XmlDocument xmldoc;
XmlNode xmlnode;
XmlElement xmlelem;

1. Create an xml file in the directory with the same name as the server:

Method 1:
Xmldoc = new XmlDocument ();
// Add the Declaration section of XML
Xmlnode = xmldoc. CreateNode (XmlNodeType. XmlDeclaration ,"","");
Xmldoc. AppendChild (xmlnode );
// Add a root element
Xmlelem = xmldoc. CreateElement ("", "Employees ","");
Xmldoc. AppendChild (xmlelem );
// Add another element
For (int I = 1; I <3; I ++)
{

XmlNode root = xmldoc. SelectSingleNode ("Employees"); // find <Employees>
XmlElement xe1 = xmldoc. CreateElement ("Node"); // create a <Node> 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 <Node> 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 <Employees> node.
}
// Save the created XML document
Xmldoc. Save (Server. MapPath ("data. xml "));

//////////////////////////////////////// ////////////

Result: A file named data. xml is generated in the directory with the same name. The content is as follows,

<? Xml version = "1.0"?>
<Employees>
<Node genre = "Li zanhong" ISBN = "2-3631-4">
<Title> CS from entry to entry </title>
<Author> Hou Jie </author>
<Price> 58.3 </price>
</Node>
<Node genre = "Li zanhong" ISBN = "2-3631-4">
<Title> CS from entry to entry </title>
<Author> Hou Jie </author>
<Price> 58.3 </price>
</Node>
</Employees>

Method 2:
XmlTextWriter xmlWriter;
String strFilename = Server. MapPath ("data1.xml ");

XmlWriter = new XmlTextWriter (strFilename, Encoding. Default); // create an xml document
XmlWriter. Formatting = Formatting. Indented;
XmlWriter. WriteStartDocument ();
XmlWriter. WriteStartElement ("Employees ");

XmlWriter. WriteStartElement ("Node ");
XmlWriter. WriteAttributeString ("genre", "lizanhong ");
XmlWriter. WriteAttributeString ("ISBN", "2-3631-4 ");

XmlWriter. WriteStartElement ("title ");
XmlWriter. WriteString ("CS from entry to entry ");
XmlWriter. WriteEndElement ();

XmlWriter. WriteStartElement ("author ");
XmlWriter. WriteString (" ");
XmlWriter. WriteEndElement ();

XmlWriter. WriteStartElement ("price ");
XmlWriter. WriteString ("58.3 ");
XmlWriter. WriteEndElement ();

XmlWriter. WriteEndElement ();

XmlWriter. Close ();
//////////////////////////////////////// ///////////////
Result:
<? Xml version = "1.0" encoding = "gb2312"?>
<Employees>
<Node genre = "Li zanhong" ISBN = "2-3631-4">
<Title> CS from entry to entry </title>
<Author> Hou Jie </author>
<Price> 58.3 </price>
</Node>
</Employees>

2. Add a node:

XmlDocument xmlDoc = new XmlDocument ();
XmlDoc. Load (Server. MapPath ("data. xml "));
XmlNode root = xmlDoc. SelectSingleNode ("Employees"); // find <Employees>
XmlElement xe1 = xmlDoc. CreateElement ("Node"); // create a <Node> Node
Xe1.SetAttribute ("genre", "Zhang San"); // you can specify the genre attribute of the node.
Xe1.SetAttribute ("ISBN", "1-1111-1"); // you can specify the ISBN attribute of the node.

XmlElement xesub1 = xmlDoc. CreateElement ("title ");
Xesub1.InnerText = "C # Getting Started help"; // set a text node
Xe1.AppendChild (xesub1); // Add it to the <Node> Node
XmlElement xesub2 = xmlDoc. CreateElement ("author ");
Xesub2.InnerText = "master ";
Xe1.AppendChild (xesub2 );
XmlElement xesub3 = xmlDoc. CreateElement ("price ");
Xesub3.InnerText = "158.3 ";
Xe1.AppendChild (xesub3 );

Root. AppendChild (xe1); // Add it to the <Employees> node.
XmlDoc. Save (Server. MapPath ("data. xml "));

//////////////////////////////////////// ////////

Result: A node is added to the original xml content. The content is as follows,

<? Xml version = "1.0"?>
<Employees>
<Node genre = "Li zanhong" ISBN = "2-3631-4">
<Title> CS from entry to entry </title>
<Author> Hou Jie </author>
<Price> 58.3 </price>
</Node>
<Node genre = "Li zanhong" ISBN = "2-3631-4">
<Title> CS from entry to entry </title>
<Author> Hou Jie </author>
<Price> 58.3 </price>
</Node>
<Node genre = "zhangsan" ISBN = "1-1111-1">
<Title> C # Getting Started </title>
<Author> expert </author>
<Price> 158.3 </price>
</Node>
</Employees>

3. Modify the node value (attributes and subnodes ):

XmlDocument xmlDoc = new XmlDocument ();
XmlDoc. Load (Server. MapPath ("data. xml "));

XmlNodeList nodeList = xmlDoc. SelectSingleNode ("Employees"). ChildNodes; // obtain all child nodes of the Employees 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") = "Zhang San") // if the genre attribute value is "Zhang San"
{
Xe. SetAttribute ("genre", "update Zhang San"); // modify the attribute to "update Zhang San"

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
}
}
}
}
XmlDoc. Save (Server. MapPath ("data. xml"); // Save.

//////////////////////////////////////// /////////////

Result: The information of all the original nodes is modified. The xml content is as follows,

<? Xml version = "1.0"?>
<Employees>
<Node genre = "Li zanhong" ISBN = "2-3631-4">
<Title> CS from entry to entry </title>
<Author> Hou Jie </author>
<Price> 58.3 </price>
</Node>
<Node genre = "Li zanhong" ISBN = "2-3631-4">
<Title> CS from entry to entry </title>
<Author> Hou Jie </author>
<Price> 58.3 </price>
</Node>
<Node genre = "update zhangsan" ISBN = "1-1111-1">
<Title> C # Getting Started </title>
<Author> Yasheng </author>
<Price> 158.3 </price>
</Node>
</Employees>

4. Modify the node (adding node attributes and adding the node's self-node ):

XmlDocument xmlDoc = new XmlDocument ();
XmlDoc. Load (Server. MapPath ("data. xml "));

XmlNodeList nodeList = xmlDoc. SelectSingleNode ("Employees"). ChildNodes; // obtain all child nodes of the Employees Node

Foreach (XmlNode xn in nodeList)
{
XmlElement xe = (XmlElement) xn;
Xe. SetAttribute ("test", "111111 ");

XmlElement xesub = xmlDoc. CreateElement ("flag ");
Xesub. InnerText = "1 ";
Xe. AppendChild (xesub );
}
XmlDoc. Save (Server. MapPath ("data. xml "));

//////////////////////////////////////// //////////////////////

Result: an attribute is added for each node, and a subnode is added as follows,

<? Xml version = "1.0"?>
<Employees>
<Node genre = "Li zanhong" ISBN = "2-3631-4" test = "111111">
<Title> CS from entry to entry </title>
<Author> Hou Jie </author>
<Price> 58.3 </price>
<Flag> 1 </flag>
</Node>
<Node genre = "Li zanhong" ISBN = "2-3631-4" test = "111111">
<Title> CS from entry to entry </title>
<Author> Hou Jie </author>
<Price> 58.3 </price>
<Flag> 1 </flag>
</Node>
<Node genre = "update zhangsan" ISBN = "1-1111-1" test = "111111">
<Title> C # Getting Started </title>
<Author> Yasheng </author>
<Price> 158.3 </price>
<Flag> 1 </flag>
</Node>
</Employees>

5. delete an attribute in a node:

XmlDocument xmlDoc = new XmlDocument ();
XmlDoc. Load (Server. MapPath ("data. xml "));
XmlNodeList xnl = xmlDoc. SelectSingleNode ("Employees"). ChildNodes;
Foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement) xn;
Xe. RemoveAttribute ("genre"); // Delete genre attributes

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 = "flag") // if
{
Xe. RemoveChild (xe2); // Delete
}
}
}
XmlDoc. Save (Server. MapPath ("data. xml "));

//////////////////////////////////////// /// //]

Result: A node attribute and a subnode of the node are deleted as follows,

<? Xml version = "1.0"?>
<Employees>
<Node ISBN = "2-3631-4" test = "111111">
<Title> CS from entry to entry </title>
<Author> Hou Jie </author>
<Price> 58.3 </price>
</Node>
<Node ISBN = "2-3631-4" test = "111111">
<Title> CS from entry to entry </title>
<Author> Hou Jie </author>
<Price> 58.3 </price>
</Node>
<Node ISBN = "1-1111-1" test = "111111">
<Title> C # Getting Started </title>
<Author> Yasheng </author>
<Price> 158.3 </price>
</Node>
</Employees>

6. delete a node:
XmlDocument xmlDoc = new XmlDocument ();
XmlDoc. Load (Server. MapPath ("data. xml "));
XmlNode root = xmlDoc. SelectSingleNode ("Employees ");
XmlNodeList xnl = xmlDoc. SelectSingleNode ("Employees"). ChildNodes;
For (int I = 0; I <xnl. Count; I ++)
{
XmlElement xe = (XmlElement) xnl. Item (I );
If (xe. GetAttribute ("genre") = "Zhang San ")
{
Root. RemoveChild (xe );
If (I <xnl. Count) I = I-1;
}
}
XmlDoc. Save (Server. MapPath ("data. xml "));

//////////////////////////////////////// ////]

Result: All nodes that meet the conditions are deleted. The original content is as follows:

<? Xml version = "1.0"?>
<Employees>
<Node genre = "Li zanhong" ISBN = "2-3631-4">
<Title> CS from entry to entry </title>
<Author> Hou Jie </author>
<Price> 58.3 </price>
</Node>
<Node genre = "Li zanhong" ISBN = "2-3631-4">
<Title> CS from entry to entry </title>
<Author> Hou Jie </author>
<Price> 58.3 </price>
</Node>
<Node genre = "zhangsan" ISBN = "1-1111-1">
<Title> C # Getting Started </title>
<Author> expert </author>
<Price> 158.3 </price>
</Node>
<Node genre = "zhangsan" ISBN = "1-1111-1">
<Title> C # Getting Started </title>
<Author> expert </author>
<Price> 158.3 </price>
</Node>
</Employees>

Deleted content:

<? Xml version = "1.0"?>
<Employees>
<Node genre = "Li zanhong" ISBN = "2-3631-4">
<Title> CS from entry to entry </title>
<Author> Hou Jie </author>
<Price> 58.3 </price>
</Node>
<Node genre = "Li zanhong" ISBN = "2-3631-4">
<Title> CS from entry to entry </title>
<Author> Hou Jie </author>
<Price> 58.3 </price>
</Node>
</Employees>

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.