10. C # knowledge point: Operating XML,

Source: Internet
Author: User

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 .............

 

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.