Create and read XML files in C #

Source: Internet
Author: User

In the project, you need to save the information on the front-end page and store it as an XML file to the database. Therefore, I will first use a small instance to learn how to create and read XML.

Part of reference http://blog.csdn.net/lengjing126/archive/2009/08/12/4438042.aspx

1. Create a simple XML file

To facilitate testing, we first create a console applicationProgramProject name: createxml, program. CSCodeAs follows:

Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> using system. XML; </P> <p> namespace createxml <br/> {<br/> class Program <br/> {<br/> static void main (string [] ARGs) <br/>{< br/> program APP = new program (); <br/> app. createxmlfile (); <br/>}< br/> Public void createxmlfile () <br/>{< br/> xmldocument xmldoc = new xmldocument (); <br/> // create a type declaration node <br/> xmlnode node = xmldoc. createxmldeclaration ("1.0", "UTF-8", ""); <br/> xmldoc. appendchild (node); <br/> // create a root node <br/> xmlnode root = xmldoc. createelement ("user"); <br/> xmldoc. appendchild (Root); <br/> createnode (xmldoc, root, "name", "xuwei"); <br/> createnode (xmldoc, root, "sex ", "male"); <br/> createnode (xmldoc, root, "Age", "25"); <br/> try <br/>{< br/> xmldoc. save ("C: // data2.xml"); <br/>}< br/> catch (exception E) <br/> {<br/> // Display Error Information <br/> console. writeline (E. message); <br/>}< br/> // console. readline (); </P> <p >}</P> <p> // <summary> <br/> // create a node <br/> // </Summary> <br/> /// <Param name = "xmldoc"> </param> XML document <br/> /// <Param name = "parentnode"> </Param> parent node <br/> /// <Param name = "name"> </param> node name <br/> /// <Param name = "value"> </param> node value <br/> /// <br/> Public void createnode (xmldocument xmldoc, xmlnode parentnode, string name, string value) <br/>{< br/> xmlnode node = xmldoc. createnode (xmlnodetype. element, name, null); <br/> node. innertext = value; <br/> parentnode. appendchild (node); <br/>}< br/>

In this case, the data2.xml file is created under the root directory of the C drive. The file content is

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <user> <br/> <Name> xuwei </Name> <br/> <sex> male </sex> <br/> <age> 25 </age> <br/> </user>

2. Create multi-node, multi-level XML files

You only need to modify the createxmlfile () method as follows:

Public void createxmlfile () <br/>{< br/> xmldocument xmldoc = new xmldocument (); <br/> // create a type declaration node <br/> xmlnode node = xmldoc. createxmldeclaration ("1.0", "UTF-8", ""); <br/> xmldoc. appendchild (node); <br/> // create a root node <br/> xmlnode root = xmldoc. createelement ("users"); <br/> xmldoc. appendchild (Root); </P> <p> xmlnode node1 = xmldoc. createnode (xmlnodetype. element, "user", null); <br/> createnode (xmldoc, node1, "name", "xuwei"); <br/> createnode (xmldoc, node1, "Sex", "male"); <br/> createnode (xmldoc, node1, "Age", "25"); <br/> root. appendchild (node1); </P> <p> xmlnode node2 = xmldoc. createnode (xmlnodetype. element, "user", null); <br/> createnode (xmldoc, node2, "name", "xiaolai"); <br/> createnode (xmldoc, node2, "Sex", "female"); <br/> createnode (xmldoc, node2, "Age", "23"); <br/> root. appendchild (node2); </P> <p> try <br/> {<br/> xmldoc. save ("C: // data5.xml"); <br/>}< br/> catch (exception E) <br/> {<br/> // Display Error Information <br/> console. writeline (E. message); <br/>}< br/> // console. readline (); </P> <p>}

The generated XML file is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <users> <br/> <user> <br/> <Name> xuwei </Name> <br/> <sex> male </sex> <br /> <age> 25 </age> <br/> </user> <br/> <Name> xiaolai </Name> <br/> <sex> female </sex> <br/> <age> 23 </age> <br/> </user> <br/> </users>

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.