This article describes how to create XML files. Share it for your reference, as follows:
Method 1: Construct XML documents step by step according to the XML structure.
Implemented through various classes encapsulated in namespace "" in the .Net FrameWork SDK
Method 1: Construct XML documents step by step according to the XML structure.
Implemented through various classes encapsulated in namespace "" in the .Net FrameWork SDK
Method 2: Fix the XML document directly and save it to the file.
Pass the "LoadXml" method in the "XmlDocument" class
.aspx foreground code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="" Inherits="Default4" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:///TR/xhtml1/DTD/"> <html xmlns="http:///1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form runat="server"> <div> <asp:Button ID="btn" runat="server" OnClick="btn1_OnClick" Text="The first way to create xml" /><br /> <asp:Button ID="btn2" runat="server" OnClick="btn2_OnClick" Text="The second way to create xml" /> </div> </form> </body> </html>
The .cs background code implementation is as follows:
using System; using ; using ; using ; using ; using ; using ; public partial class Default4 : { 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(); //Addition paragraph to add XML XmlNode xmlnode = ("1.0", "gb2312", null); (xmlnode); //Add a root element XmlElement xmlelem = ("", "bookstore", ""); xmltext = (""); (xmltext); (xmlelem); //Add a child element XmlElement xmlelem1 = ("", "book", ""); xmltext = (""); (xmltext); //Add two attributes to the child element "book" ("genre", "", "fantasy"); ("ISBN", "2-3631-4"); (1).AppendChild(xmlelem1); //Create the child elements of three child elements XmlElement xmlelem2 = ("", "title", ""); xmltext = ("Oberon's Legacy"); (xmltext); (1).AppendChild(xmlelem1).AppendChild(xmlelem2); XmlElement xmlelem3 = ("", "author", ""); xmltext = ("Corets, Eva"); (xmltext); (1).AppendChild(xmlelem1).AppendChild(xmlelem3); XmlElement xmlelem4 = ("", "price", ""); xmltext = ("5.95"); (xmltext); (1).AppendChild(xmlelem1).AppendChild(xmlelem4); (("")); //save } //Create xml file method two protected void btn2_OnClick(object sender, EventArgs e) { XmlDocument xmldoc = new XmlDocument(); //Create an empty XML document ("<?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>"); (("")); //save } }
Comparison: The first one is more flexible to create, while the second one is more convenient to create. The final xml file is as follows: (The creation effect of both methods is 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 2: Fix the XML document directly and save it to the file.
Pass the "LoadXml" method in the "XmlDocument" class
.aspx foreground code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="" Inherits="Default4" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:///TR/xhtml1/DTD/"> <html xmlns="http:///1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form runat="server"> <div> <asp:Button ID="btn" runat="server" OnClick="btn1_OnClick" Text="The first way to create xml" /><br /> <asp:Button ID="btn2" runat="server" OnClick="btn2_OnClick" Text="The second way to create xml" /> </div> </form> </body> </html>
The .cs background code implementation is as follows:
using System; using ; using ; using ; using ; using ; using ; public partial class Default4 : { 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(); //Addition paragraph to add XML XmlNode xmlnode = ("1.0", "gb2312", null); (xmlnode); //Add a root element XmlElement xmlelem = ("", "bookstore", ""); xmltext = (""); (xmltext); (xmlelem); //Add a child element XmlElement xmlelem1 = ("", "book", ""); xmltext = (""); (xmltext); //Add two attributes to the child element "book" ("genre", "", "fantasy"); ("ISBN", "2-3631-4"); (1).AppendChild(xmlelem1); //Create the child elements of three child elements XmlElement xmlelem2 = ("", "title", ""); xmltext = ("Oberon's Legacy"); (xmltext); (1).AppendChild(xmlelem1).AppendChild(xmlelem2); XmlElement xmlelem3 = ("", "author", ""); xmltext = ("Corets, Eva"); (xmltext); (1).AppendChild(xmlelem1).AppendChild(xmlelem3); XmlElement xmlelem4 = ("", "price", ""); xmltext = ("5.95"); (xmltext); (1).AppendChild(xmlelem1).AppendChild(xmlelem4); (("")); //save } //Create xml file method two protected void btn2_OnClick(object sender, EventArgs e) { XmlDocument xmldoc = new XmlDocument(); //Create an empty XML document ("<?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>"); (("")); //save } }
Comparison: The first one is more flexible to create, while the second one is more convenient to create. The final xml file is as follows: (The creation effect of both methods is 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>
For more information about relevant content, please check out the topic of this site:Summary of operating XML skills》、《Summary of file operation skills》、《Ajax tips summary topic"and"Summary of cache operation skills》。
I hope this article will be helpful to everyone's programming.