How to operate XML (zt) in C #. net)

Source: Internet
Author: User

Http://weekzero.cnblogs.com/articles/178140.html
How to operate XML in C #. net
Namespace to be added:
Using system. xml;

Define several public objects:
Xmldocument xmldoc;
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, <? XML version = "1.0" encoding = "gb2312"?>
Xmldeclaration xmldecl;
Xmldecl = xmldoc. createxmldeclaration ("1.0", "gb2312", null );
Xmldoc. appendchild (xmldecl );

// 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" 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>
<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" 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>
<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: modify the information of all original nodes. The XML content is as follows:



CS entry-level knowledge </ title> <br> <author> Hou Jie </author> <br> <price> 58.3 </price> <br> </node> <br> <node genre =" li zanhong "ISBN =" 2-3631-4 "> <br> <title> CS entry to proficiency
Hou Jie
58.3


C # Getting Started
Yasheng
158.3

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" encoding = "gb2312"?>
<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. The content is as follows:



CS entry-level knowledge
Hou Jie
58.3


CS entry to proficiency
Hou Jie
58.3


C # Getting Started
Yasheng
158.3

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" 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>
<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 Getting Started </title>
<Author> expert </author>
<Price> 158.3 </price>
</Node>
</Employees>

Deleted content:
<? 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>
<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>

7. Read XML according to text files

System. Io. streamreader myfile = new
System. Io. streamreader (server. mappath ("data. xml"), system. Text. encoding. Default );
// Note system. Text. encoding. Default

String mystring = myfile. readtoend (); // mystring is the read string
Myfile. Close ();

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.