10. C # knowledge point: Operating XML,
Knowledge Point directory ==========> Portal
XML does not need to be a text markup language.
It mainly records how to add, delete, modify, and query XML files.
Xml operation classes all exist under the System. xml namespace.
Application-oriented Code directly
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. xml; namespace XMLTest {class Program {static void Main (string [] args) {// 1. create an XML Document Object XmlDocument doc = new XmlDocument (); // create the header XmlDeclaration xmlDeclaration = doc. createXmlDeclaration ("1.0", "UTF-8", null); // Add node doc. appendChild (xmlDeclaration); XmlElement xmlElement = doc. createElement ("Persons"); // Add the xmlElement attribute to the node. setAttribute ("Name", "one hour Superman"); doc. appendChild (xmlElement); XmlElement xmlElement1 = doc. createElement ("Person"); // Add the text xmlElement1.InnerXml = "Superman" to the node; xmlElement. appendChild (xmlElement1); doc. save ("Test. xml ");}}}
<? Xml version = "1.0" encoding = "UTF-8"?> <Persons Name = "one hour Superman"> <Person> Superman </Person> </Persons>
This section mainly describes the differences between XmlElement. InnerXml and XmlElement. InnerText. Code demo
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. xml; namespace XMLTest {class Program {static void Main (string [] args) {// 1. create an XML Document Object XmlDocument doc = new XmlDocument (); // create the header XmlDeclaration xmlDeclaration = doc. createXmlDeclaration ("1.0", "UTF-8", null); // Add node doc. appendChild (xmlDeclaration); XmlElement xmlElement = doc. createElement ("Persons"); // Add the xmlElement attribute to the node. setAttribute ("Name", "one hour Superman"); doc. appendChild (xmlElement); XmlElement xmlElement1 = doc. createElement ("Person"); // Add text xmlElement1.InnerXml = "<demo> Superman </demo>"; xmlElement. appendChild (xmlElement1); XmlElement xmlElement2 = doc. createElement ("Person"); // Add the text xmlElement2.InnerText = "<demo> Superman </demo> to the node ";
// Add attributes to a node
XmlElement2.SetAttribute ("name", "one hour Superman ");
xmlElement.AppendChild(xmlElement2); doc.Save("Test.xml"); } }}
<? Xml version = "1.0" encoding = "UTF-8"?> <Persons Name = "one hour Superman"> <Person> <demo> Superman </demo> </Person> <Person name = "one hour Superman"> & lt; demo & gt; superman </demo> </Person> </Persons>
Obviously, if the string is a tag, Interxml will be added as a tag, and innterText will be escaped.
The following shows how to read data.
Using System; using System. collections. generic; using System. IO; using System. linq; using System. text; using System. threading. tasks; using System. xml; namespace XMLTest {class Program {static void Main (string [] args) {// 1. create an XML Document Object XmlDocument doc = new XmlDocument (); if (File. exists ("Test. xml ") {// you can load Xml by file name, stream, or other overload methods. Doc. load ("Test. xml "); // get the root node XmlElement xmlElement = doc. documentElement; // obtain the subnode set XmlNodeList nodeList = xmlElement under the root node. childNodes; // cyclically fetch each subnode foreach (XmlNode item in nodeList) {Console. writeLine (item. name); // get node attributes // string attributesValue = item. attributes ["attribute name"]. value;} Console. readKey ();}}}}
The code above lists common operations and many other operations. I will not list them one by one .............