C#. NET operations XML method one

Source: Internet
Author: User

We know that XML is a markup language that is used to tag data, define data types, and is a source language that executes the user's definition of their markup language. Because of the good structure. and easy to understand, like a tree, hierarchical relationship is clear, so often some data stored in XML files, the following is how to manipulate the XML file in C #.


Frequently used classes

Xmldocument:xml's documentation. It's like a tree.

the node class in Xmlnode:xml is like a branch.

The elements in the xmlelement:xml are like the leaves on the branches.


Now that we've learned about these often-used classes, is it really easy to draw a tree?

The tree ......... .......... Branches.......................... Leaves


The following for everyone to demonstrate operation again, first look at the finally


<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" ><span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" ><?xml version= "1.0" encoding= "gb2312"?><employees> <node name=  "Li Hong" age= ">"    < Hobby> Basketball 


In the code above, employees has two nodes below. And there are two of elements under each node. How to configure these elements? Code such as the following


<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" ><span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;"            >//references two variables XmlDocument xmldoc;            XmlElement Xmlelem; xmldoc = new XmlDocument ()/////Join the declaration paragraph of XML <?xml version= "1.0" encoding= "gb2312"?> Xmldeclarati            On XMLDECL; Xmldecl = xmldoc.            Createxmldeclaration ("1.0", "gb2312", null); Add the xmldoc to the XML file.            AppendChild (XMLDECL); Create an element Xmlelem = xmldoc.            CreateElement ("", "Employees", ""); XmlDoc.            AppendChild (Xmlelem); for (int i = 1; i < 3; i++) {//Find Employees node in document XmlNode root = xmldoc.                selectSingleNode ("Employees"); Create an element XmlElement Xel = xmldoc.                CreateElement ("Node"); Sets the attribute xel of the node element.                SetAttribute ("name", "Li Hong"); Xel.                SetAttribute ("Age", "23"); Create an additional element Xmlelement xesub1 = xmldoc.                CreateElement ("hobby"); Sets the text content displayed xesub1.                InnerText = "basketball"; Add elements to the Xel Xel.                AppendChild (XESUB1); XmlElement xesub2 = xmldoc.                createelement ("Games"); Xesub2.                InnerText = "QQ speed"; Xel.                AppendChild (XESUB2); Root.                AppendChild (Xel); Finally, the XML file has the name of data saved to the same folder under the name of the server xmldoc. Save (Server.MapPath ("Data.xml"));</span></span>




Summary

The whole process, in fact, is achieved through a few classes under the System.Xml namespace in C #, only to be familiar with the properties and methods of these classes. Actually, it's easy. The above is simply the creation of operations, through the properties and methods of these classes we can also be changed, delete and other operations.




????

C#. NET operations XML method one

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.