ASP. NET create XML file method

Source: Internet
Author: User

Method One: Build the XML document step-by-step according to the structure of the XML.
Implemented through the various classes encapsulated in the namespace "System.Xml" in the. Net FrameWork SDK

Method One: Build the XML document step-by-step according to the structure of the 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 ">

<title></title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:button id= "BTN" runat= "Server" onclick= "Btn1_onclick" text= "first method of creating XML"/><br/>
<asp:button id= "BTN2" runat= "Server" onclick= "Btn2_onclick" text= "The second method of creating XML"/>
</div>
</form>
</body>

The. 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)
{

}
Create an XML file method one
protected void Btn1_onclick (object sender, EventArgs e)
{
XmlText XmlText;
XmlDocument xmldoc = new XmlDocument ();

Add a declaration paragraph to 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);
Add two properties to the child element "book"
Xmlelem1. SetAttribute ("Genre", "" "," Fantasy ");
Xmlelem1. SetAttribute ("ISBN", "2-3631-4");

XmlDoc. Childnodes.item (1). AppendChild (XMLELEM1);

Create 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 XmlDocument (); To create 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>" +
"<price>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 method creation effects are the same)

<?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 ">

<title></title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:button id= "BTN" runat= "Server" onclick= "Btn1_onclick" text= "first method of creating XML"/><br/>
<asp:button id= "BTN2" runat= "Server" onclick= "Btn2_onclick" text= "The second method of creating XML"/>
</div>
</form>
</body>

The. 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)
{

}
Create an XML file method one
protected void Btn1_onclick (object sender, EventArgs e)
{
XmlText XmlText;
XmlDocument xmldoc = new XmlDocument ();

Add a declaration paragraph to 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);
Add two properties to the child element "book"
Xmlelem1. SetAttribute ("Genre", "" "," Fantasy ");
Xmlelem1. SetAttribute ("ISBN", "2-3631-4");

XmlDoc. Childnodes.item (1). AppendChild (XMLELEM1);

Create 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 XmlDocument (); To create 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>" +
"<price>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 method creation effects are the same)

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

ASP. NET create XML file method

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.