The examples in this article describe the ASP.net method for simply generating XML files. Share to everyone for your reference, specific as follows:
Way one: Use DataSet directly
SqlConnection conn = new SqlConnection ();
Conn. ConnectionString = "server=127.0.0.1; User Id=sa; Password=sa;database=northwind; Persist security Info=true ";
Conn. Open ();
SqlDataAdapter da = new SqlDataAdapter ("Select * FROM table", conn);
SqlCommandBuilder Thisbulder = new SqlCommandBuilder (DA);
DataSet ds = new DataSet ();
Da. Fill (DS);
Ds. WriteXml (@ "C:/temp.xml");
Mode two: Custom Build Way
The using system.xml;//header plus this namespace
xmldocument xd = new XmlDocument ();//Represents the XML document
XmlDeclaration xde;//represents the XML declaration node:< ? xml version= ' 1.0 ' ...? >
xde = xd. Createxmldeclaration ("1.0", "GBK", null), or the second item of the parameter is encoding
//standalone defines whether the document can be processed without reading any other files, and the default is no
xd. AppendChild (XDE);//<?xml version= "1.0" encoding= "UTF-8" standalone= "yes"?> generate end
XmlElement XE = xd. createelement ("root");//create a root root element
xd. AppendChild (XE);//root root element creation Complete
XmlNode root = xd. selectSingleNode ("Root");//Lookup <Root>
XmlElement xe1 = xd. createelement ("tree")//Create element <Tree> Xe1 under <Root>
. SetAttribute ("id", "1");//Specifies the property value of the property
Xe1. innertext = "Type 1";//Specifies the attribute text node
root. AppendChild (XE1)//Finish child node <Tree>
XD. Save (Server.MapPath ("Xml.xml"));
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.