1. Brief Introduction
Using system. xml;
// Initialize an XML instance
Xmldocument xml = new xmldocument ();
// Import the specified XML file
XML. Load (PATH );
XML. Load (httpcontext. Current. server. mappath ("~ /File/bookstore. xml "));
// Specify a node
Xmlnode root = xml. selectsinglenode ("/root ");
// Obtain all direct subnodes under a node
Xmlnodelist childlist = root. childnodes;
// Determine whether a subnode exists under the node
Root. haschildnodes;
// Obtain the set of nodes with the same name as the peer node
Xmlnodelist nodelist = xml. selectnodes ("/root/News ");
// Generate a new node
Xmlelement node = xml. createelement ("news ");
// Add a node to a specified node as its subnode
Root. appendchild (node );
// Add the node to a subnode under the specified Node
Root. insertbefore (node, root. childenodes [I]);
// Create a property for the specified node and assign a value to it
Node. setattribute ("ID", "11111 ");
// Add a subnode to a specified Node
Root. appendchild (node );
// Obtain the specified attribute value of a specified Node
String id = node. attributes ["ID"]. value;
// Obtain the text in the specified Node
String content = node. innertext;
// Save the XML file
String Path = server. mappath ("~ /File/bookstore. xml ");
XML. Save (PATH );
// Or use: XML. Save (httpcontext. Current. server. mappath ("~ /File/bookstore. xml "));
II. Specific instance
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: The information of all the original nodes is modified. The XML 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 = "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" 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 as follows,
<? XML version = "1.0" encoding = "gb2312"?>
<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" 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 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 based on 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 ();
3. Advanced Applications
/* Two XML methods for reading XML data */
<AAA>
<BB> something </BB>
<CC> something </CC>
</AAA>
<AAA>
<Add key = "123" value = "321"/>
</AAA>
/* Method 1 */
DS. readxml ("Your xmlfile name ");
Container. dataitem ("BB ");
Container. dataitem ("cc ");
DS. readxmlschema ("Your xmlfile name ");
/* Method 2 */
<AAA>
<Add key = "123" value = "321"/>
</AAA>
What should I do if I want to find 123 and get 321?
Using system. xml;
Xmldatadocument xmldoc = new system. xml. xmldatadocument ();
Xmldoc. Load (@ "C:/config. xml ");
Xmlelement ELEM = xmldoc. getelementbyid ("add ");
String STR = ELEM. attributes ["value"]. Value
/* Method 3: selectsinglenode reads XML in two formats *---/
--------------------------------------------------------------------
<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Deleetask>
<Connectionstring> Data Source = YF; user id = ctm_dbo; Password = 123 </connectionstring>
</Appsettings>
</Configuration>
--------------------------------------------------------------------------
Xmldocument Doc = new xmldocument ();
Doc. Load (strxmlname );
Xmlnode node = Doc. selectsinglenode ("/configuration/etettings/connectionstring ");
If (node! = NULL)
{
String k1 = node. value; // null
String k2 = node. innertext; // data source = YF; user id = ctm_dbo; Password = 123
String K3 = node. innerxml; // data source = YF; user id = ctm_dbo; Password = 123
Node = NULL;
}
**************************************** ****************************
<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Deleetask>
<Add key = "connectionstring" value = "Data Source = YF; user id = ctm_dbo; Password = 123"/>
</Appsettings>
</Configuration>
**--------------------------------------------------------------------**
Xmlnode node = Doc. selectsinglenode ("/configuration/etettings/Add ");
If (node! = NULL)
{
String K = node. attributes ["key"]. value;
String v = node. attributes ["value"]. value;
Node = NULL;
}
*--------------------------------------------------------------------*
Xmlnode node = Doc. selectsinglenode ("/configuration/etettings/Add ");
If (node! = NULL)
{
Xmlnodereader Nr = new xmlnodereader (node );
Nr. movetocontent ();
// Check whether the current node is a content node. If this node is not a content node, the reader jumps forward to the next content node or the end of the file.
Nr. movetoattribute ("value ");
String S = Nr. value;
Node = NULL;
}