C # Knowledge Points: Manipulating XML

Source: Internet
Author: User

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 &GT, 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" >&lt; demo &gt; Little Superman &lt;/demo &gt;</ 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.