asp.net how to create an XML file summary _ Practical Tips

Source: Internet
Author: User
Tags joins

The example in this article describes how asp.net creates an XML file. Share to everyone for your reference, specific as follows:

Method One: Build XML documents Step-by-step according to the structure of XML.

Implemented through the various classes encapsulated in the namespace "System.Xml" in the. Net FrameWork SDK

Method One: Build XML documents Step-by-step according to the structure of XML.

Implemented through the various classes encapsulated in the namespace "System.Xml" in the. Net FrameWork SDK

Method Two: Directly fuser the XML document, and then save to the file.

Through the "Loadxml" method in the "XmlDocument" class

. aspx foreground code:

<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default4.aspx.cs" inherits= "Default4"%> <!
DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
 
 

. CS background code is implemented as follows:

Using System;
Using System.Collections.Generic;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Text;
Using System.Xml; Public partial class Default4:System.Web.UI.Page {protected void Page_Load (object sender, EventArgs e) {}//Chuang
     Build XML file method one protected void Btn1_onclick (object sender, EventArgs e) {XmlText XmlText;
    XmlDocument xmldoc = new XmlDocument (); The declaration paragraph that joins the XML XmlNode XmlNode = xmldoc.
     Createxmldeclaration ("1.0", "gb2312", null); XmlDoc.
    AppendChild (XmlNode); Add a root element xmlelement Xmlelem = xmldoc.
     CreateElement ("", "bookstore", ""); XmlText = xmldoc.
     createTextNode (""); Xmlelem.
     AppendChild (xmltext); XmlDoc.
    AppendChild (Xmlelem); Add a child element XmlElement xmlelem1 = xmldoc.
     CreateElement ("", "book", ""); XmlText = xmldoc.
     createTextNode (""); Xmlelem1.
    AppendChild (xmltext); Adds two attribute xmlelem1 to the child element "book".
     SetAttribute ("Genre", "" "," Fantasy "); Xmlelem1. SetAttribute ("ISBN "," 2-3631-4 "); XmlDoc. Childnodes.item (1).
    AppendChild (XMLELEM1); Creates a child element of three child elements XmlElement xmlelem2 = xmldoc.
     CreateElement ("", "title", ""); XmlText = xmldoc.
    createTextNode ("Oberon ' s Legacy"); Xmlelem2.
     AppendChild (xmltext); XmlDoc. Childnodes.item (1). AppendChild (XMLELEM1).
     AppendChild (XMLELEM2); XmlElement xmlelem3 = xmldoc.
     CreateElement ("", "Author", ""); XmlText = xmldoc.
     createTextNode ("Corets, Eva"); Xmlelem3.
     AppendChild (xmltext); XmlDoc. Childnodes.item (1). AppendChild (XMLELEM1).
     AppendChild (XMLELEM3); XmlElement xmlelem4 = xmldoc.
     CreateElement ("", "Price", ""); XmlText = xmldoc.
     createTextNode ("5.95"); Xmlelem4.
     AppendChild (xmltext); XmlDoc. Childnodes.item (1). AppendChild (XMLELEM1).
     AppendChild (XMLELEM4); XmlDoc. Save (Server.MapPath ("Bookstore.xml")); Save}//Create XML file method two protected void Btn2_onclick (object sender, EventArgs e) {XmlDocument xmldoc = new Xmldo Cument (); Creates an empty XML document XMLDOC. Loadxml (";?  XML version= ' 1.0 ' encoding= ' gb2312 '?> ' + ' <bookstore> ' + ' <book genre= ' fantasy ' isbn= ' 2-3631-4 ' > ' + "<title>oberon ' s legacy</title>" + "<author>corets, eva</author>" + "&LT;PRICE&G
     T;5.95</price> "+" </book> "+" </bookstore> "); XmlDoc. Save (Server.MapPath ("Bookstore2.xml"));

 Save}}

Comparison: The first one is more flexible to create, and the second is more convenient to create. The resulting XML file is as follows: (two methods create the same effect)

<?xml version= "1.0" encoding= "gb2312"?>
<bookstore>
 <book genre= "Fantasy" isbn= "2-3631-4" >
 <title>oberon ' s legacy</title>
 <author>corets, eva</author>
 <price >5.95</price>
 </book>
</bookstore>

Method Two: Directly fuser the XML document, and then save to the file.

Through the "Loadxml" method in the "XmlDocument" class
. aspx foreground code:

<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default4.aspx.cs" inherits= "Default4"%> <!
DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
 
 

. CS background code is implemented as follows:

Using System;
Using System.Collections.Generic;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Text;
Using System.Xml; Public partial class Default4:System.Web.UI.Page {protected void Page_Load (object sender, EventArgs e) {}//Chuang
     Build XML file method one protected void Btn1_onclick (object sender, EventArgs e) {XmlText XmlText;
    XmlDocument xmldoc = new XmlDocument (); The declaration paragraph that joins the XML XmlNode XmlNode = xmldoc.
     Createxmldeclaration ("1.0", "gb2312", null); XmlDoc.
    AppendChild (XmlNode); Add a root element xmlelement Xmlelem = xmldoc.
     CreateElement ("", "bookstore", ""); XmlText = xmldoc.
     createTextNode (""); Xmlelem.
     AppendChild (xmltext); XmlDoc.
    AppendChild (Xmlelem); Add a child element XmlElement xmlelem1 = xmldoc.
     CreateElement ("", "book", ""); XmlText = xmldoc.
     createTextNode (""); Xmlelem1.
    AppendChild (xmltext); Adds two attribute xmlelem1 to the child element "book".
     SetAttribute ("Genre", "" "," Fantasy "); Xmlelem1. SetAttribute ("ISBN "," 2-3631-4 "); XmlDoc. Childnodes.item (1).
    AppendChild (XMLELEM1); Creates a child element of three child elements XmlElement xmlelem2 = xmldoc.
     CreateElement ("", "title", ""); XmlText = xmldoc.
    createTextNode ("Oberon ' s Legacy"); Xmlelem2.
     AppendChild (xmltext); XmlDoc. Childnodes.item (1). AppendChild (XMLELEM1).
     AppendChild (XMLELEM2); XmlElement xmlelem3 = xmldoc.
     CreateElement ("", "Author", ""); XmlText = xmldoc.
     createTextNode ("Corets, Eva"); Xmlelem3.
     AppendChild (xmltext); XmlDoc. Childnodes.item (1). AppendChild (XMLELEM1).
     AppendChild (XMLELEM3); XmlElement xmlelem4 = xmldoc.
     CreateElement ("", "Price", ""); XmlText = xmldoc.
     createTextNode ("5.95"); Xmlelem4.
     AppendChild (xmltext); XmlDoc. Childnodes.item (1). AppendChild (XMLELEM1).
     AppendChild (XMLELEM4); XmlDoc. Save (Server.MapPath ("Bookstore.xml")); Save}//Create XML file method two protected void Btn2_onclick (object sender, EventArgs e) {XmlDocument xmldoc = new Xmldo Cument (); Creates an empty XML document XMLDOC. Loadxml (";?  XML version= ' 1.0 ' encoding= ' gb2312 '?> ' + ' <bookstore> ' + ' <book genre= ' fantasy ' isbn= ' 2-3631-4 ' > ' + "<title>oberon ' s legacy</title>" + "<author>corets, eva</author>" + "&LT;PRICE&G
     T;5.95</price> "+" </book> "+" </bookstore> "); XmlDoc. Save (Server.MapPath ("Bookstore2.xml"));

 Save}}

Comparison: The first one is more flexible to create, and the second is more convenient to create. The resulting XML file is as follows: (two methods create the same effect)

<?xml version= "1.0" encoding= "gb2312"?>
<bookstore>
 <book genre= "Fantasy" isbn= "2-3631-4" >
 <title>oberon ' s legacy</title>
 <author>corets, eva</author>
 <price >5.95</price>
 </book>
</bookstore>

More interested readers of asp.net related content can view the site topics: "asp.net operation XML Skills summary", "asp.net file Operation skills Summary", "ASP.net Ajax Skills Summary topic" and "asp.net cache operation skills Summary."

I hope this article will help you to ASP.net program design.

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.