How to operate XML in C # .NET requires the namespace to add: use system.xml;
Define several public objects: xmldocument Xmldoc; XMLNode XMLNode; XMLELEMENT XMLEM;
1. Create an XML file to the server's same name directory:
Method 1: Xmldoc = new xmldocument (); // Add XML declaration paragraph, 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 ("" "" ",", "); xmldoc.Appendchild (xmlelem); // Add another element for (INT i = 1; i <3; i ) {
XMLNode root = xmldoc.selectsinglenode ("Employees"); // Find
XMLELEMENT XESUB1 = XMLDoc.createElement ("Title"); Xesub1.innertext = "CS From Getting Started to Wen"; // Settings Text Node XE1.Appendchild (Xesub1); // Add to
Root.appendchild (xe1); // Add to
/ / The content named Data.xml is generated in the directory of the same name. The content is as follows, XML Version = "1.0" Encoding = "GB2312"?> 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", "Li Zanli"); XMLWriter.writeAttributeString ("ISBN", "2-3631-4"); XMLWRITER.WRITESTARTELEMENT ("Title"); XMLWRITER.WRITESTRING ("CS from getting started to proficiency"); xmlwriter.writeEndelement (); XMLWRITER.WRITESTARTELEMENT ("Author"); XMLWRITER.WRITESTRING ("Waiting"); xmlwriter.writeEndelement (); XMLWRITER.WRITESTARTELEMENT ("Price"); XMLWRITER.WRITESTRING ("58.3"); XMLWRITER.WRITEENDEEMENT (); Xmlwriter.writeEndelement (); Xmlwriter.close (); // Result: XML Version = "1.0" Encoding = "GB2312"?> XMLELEMENT XESUB1 = XMLDoc.createElement ("Title"); Xesub1.innertext = "Getting Started Help"; // Settings Text Node Xe1.Appendchild (Xesub1); // Add to Root.Appendchild (xe1); // Add to / / The result is added to the original content of XML. The content is as follows, XML Version = "1.0" encoding = "GB2312"?> XMLNodelist NodeList = XmLDoc.selectsinglenode ("Employees"). ChildNodes; // Get all child nodes in the Employees node Foreach (XMLNode Xn In nodelist) // Traverse all child nodes {xmlelement XE = (xmlelement) XN; // convert the child node type to XMLELEMENT type if (Xe.GetaTribute ("genre") == "Zhang San") / / If the GENRE property value is "Zhang 3" {xe.setttribute ("genre", "Update Zhang 3"); // Modify this property is "Update Zhang 3" XMLNodelist NLS = XE.ChildNodes; // Continue all child nodes of the XE child node Foreach (XMLNode XN1 in NLS) // Traversed {XMLELEMENT XE2 = (XMLELEMENT) XN1; // Conversion Type IF (Xe2.Name == "AUTHOR ") // If you find {xe2.innerText =" Assembly "; // modify}}}} xmldoc.save (Server.MAppath (" DATA.XML ")); // Save. / / The information of all the nodes has been modified, and the contents of XML are as follows, XML Version = "1.0" encoding = "GB2312"?> XMLNodelist NodeList = XmLDoc.selectsinglenode ("Employees"). ChildNodes; // Get all child nodes in 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"); / /: The attribute of each node has added one, the sub-node has also added one, the content is as follows, XML Version = "1.0" Encoding = "GB2312"?> XMLNodelist NLS = XE.ChildNodes; // Continue all child nodes of the XE child node Foreach (XMLNode XN1 in NLS) // Traversed {XMLELEMENT XE2 = (XMLELEMENT) XN1; // Conversion Type IF (XE2.Name == "flag ") // If you find {xe.removechild (xe2); // remove}}} xmldoc.save (Server.MAppath (" DATA.XML ")); //] Result: Delete a sub-node of an attribute and node of the node, the content is as follows, XML Version = "1.0" encoding = "GB2312"?> //] Result: Delete all the nodes of the eligible, the original content: XML Version = "1.0" encoding = "gb2312"?> 7, read XML according to the text file System.IO.StreamReader myFile = new System.IO.StreamReader (Server.MapPath ( "data.xml"), System.Text.Encoding.Default); // Note System.Text.Encoding.Defaultstring myString = myFile.ReadToEnd ( ); // mystring is a read string myfile.close ();