What XML is, needless to say, is the text markup language.
The main record is how to add and revise XML files.
The operation classes for XML exist under the System.Xml namespace.
Application-oriented direct-on-code
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 the node doc. AppendChild (XmlDeclaration); XmlElement XmlElement = doc. CreateElement ("Persons"); Add attribute xmlelement.setattribute ("Name", "One Hour Little Superman") to the node; Doc. AppendChild (xmlElement); XmlElement xmlElement1 = doc. createelement ("person"); Add text to a node xmlelement1.innerxml = "Little Superman"; Xmlelement.appendchild (xmlElement1); Doc. Save ("Test.xml");}}}
<?xml version= "1.0" encoding= "UTF-8"? ><persons name= "One Hour Little Superman" > <Person> Little Superman </Person> </Persons>
This place mainly tells the difference 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. Gen Build XML Document Object XmlDocument doc = new XmlDocument (); Create the header XmlDeclaration XmlDeclaration = doc. Createxmldeclaration ("1.0", "UTF-8", null); Add the node doc. AppendChild (XmlDeclaration); XmlElement XmlElement = doc. CreateElement ("Persons"); Add attribute Xmlelement.setattribute ("Name", "One Hour Little Superman") to the node; Doc. AppendChild (xmlElement); XmlElement xmlElement1 = doc. createelement ("person"); Add text to a node Xmlelement1.innerxml = "< demo > Small Superman </demo >"; Xmlelement.appendchild (XMLELEMENT1); XmlElement XmlElement2 = doc. createelement ("person"); Add text to a node Xmlelement2.innertext = "< Demo >, Little Superman </demo > ";
Adding attributes to a node
Xmlelement2.setattribute ("name", "One Hour Little Superman");
Xmlelement.appendchild (XmlElement2); Doc. Save ("Test.xml");}}}
<?xml version= "1.0" encoding= "UTF-8"? ><persons name= "One Hour Little Superman" > <Person> < demo > Little Superman </demo > </Person> <person name= "one Hour Little Superman" >< demo > Little Superman </demo ></ Person></persons>
Obviously, if the string is a label, Interxml will add it to you as a label, and Inntertext will escape.
The following shows the read operation
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. Creating an XML Document object XmlDocument doc = new XmlDocument (); if (file.exists ("Test.xml")) {//The XML is loaded through the file name, you can also see the document through other overloaded methods such as streaming. Doc. Load ("Test.xml"); Gets the root node XmlElement XmlElement = Doc. DocumentElement; Gets the collection of child nodes under the root node xmlnodelist nodeList = Xmlelement.childnodes; Loop through each child node foreach (XmlNode item in nodeList) {Console.WriteLine (ITEM.N AME); Gets the node properties//string attributesvalue=item. Attributes["property name"]. Value; } console.readkey (); } } }}
C # Knowledge points: Manipulating XML