asp. net Summary of Methods for Creating XML Files
- 2021-07-18 07:45:22
- OfStack
This article illustrates how asp. net creates an XML file. Share it for your reference, as follows:
Method 1: Build the XML document step by step according to the structure of XML.
Is implemented by various classes encapsulated in the namespace "System. Xml" in. Net FrameWork SDK
Method 1: Build XML document step by step according to the structure of XML.
Is implemented by various classes encapsulated in the namespace "System. Xml" in. Net FrameWork SDK
Method 2: Fix the XML document directly and save it to a 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">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btn" runat="server" OnClick="btn1_OnClick" Text=" Create xml The first part of 1 Methods " /><br />
<asp:Button ID="btn2" runat="server" OnClick="btn2_OnClick" Text=" Create xml The first part of 2 Methods " />
</div>
</form>
</body>
</html>
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 xml File method 1
protected void btn1_OnClick(object sender, EventArgs e)
{
XmlText xmltext;
XmlDocument xmldoc = new XmlDocument();
// Join XML Statement paragraph of
XmlNode xmlnode = xmldoc.CreateXmlDeclaration("1.0", "gb2312", null);
xmldoc.AppendChild(xmlnode);
// Join 1 Root element
XmlElement xmlelem = xmldoc.CreateElement("", "bookstore", "");
xmltext = xmldoc.CreateTextNode("");
xmlelem.AppendChild(xmltext);
xmldoc.AppendChild(xmlelem);
// Join 1 Child element
XmlElement xmlelem1 = xmldoc.CreateElement("", "book", "");
xmltext = xmldoc.CreateTextNode("");
xmlelem1.AppendChild(xmltext);
// Is a child element "book" Add two attributes
xmlelem1.SetAttribute("genre", "", "fantasy");
xmlelem1.SetAttribute("ISBN", "2-3631-4");
xmldoc.ChildNodes.Item(1).AppendChild(xmlelem1);
// Create 3 Child element of child element
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 2
protected void btn2_OnClick(object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument(); // 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 type is more flexible to create, while the second type is more convenient to create. The final xml file created is as follows: (Both 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 2: Fix the XML document directly and save it to a 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">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btn" runat="server" OnClick="btn1_OnClick" Text=" Create xml The first part of 1 Methods " /><br />
<asp:Button ID="btn2" runat="server" OnClick="btn2_OnClick" Text=" Create xml The first part of 2 Methods " />
</div>
</form>
</body>
</html>
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 xml File method 1
protected void btn1_OnClick(object sender, EventArgs e)
{
XmlText xmltext;
XmlDocument xmldoc = new XmlDocument();
// Join XML Statement paragraph of
XmlNode xmlnode = xmldoc.CreateXmlDeclaration("1.0", "gb2312", null);
xmldoc.AppendChild(xmlnode);
// Join 1 Root element
XmlElement xmlelem = xmldoc.CreateElement("", "bookstore", "");
xmltext = xmldoc.CreateTextNode("");
xmlelem.AppendChild(xmltext);
xmldoc.AppendChild(xmlelem);
// Join 1 Child element
XmlElement xmlelem1 = xmldoc.CreateElement("", "book", "");
xmltext = xmldoc.CreateTextNode("");
xmlelem1.AppendChild(xmltext);
// Is a child element "book" Add two attributes
xmlelem1.SetAttribute("genre", "", "fantasy");
xmlelem1.SetAttribute("ISBN", "2-3631-4");
xmldoc.ChildNodes.Item(1).AppendChild(xmlelem1);
// Create 3 Child element of child element
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 2
protected void btn2_OnClick(object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument(); // 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 type is more flexible to create, while the second type is more convenient to create. The final xml file created is as follows: (Both 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>
For more readers interested in asp. net, please check the topics on this site: "Summary of asp. net Operation Skills", "Summary of asp. net File Operation Skills", "Summary of asp. net ajax Skills" and "Summary of asp. net Cache Operation Skills".
I hope this paper is helpful to everyone's asp. net program design.