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