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 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;
}