asp. net A simple method for generating XML files
- 2021-07-18 07:47:41
- OfStack
This article gives an example of how asp. net generates an XML file. Share it for your reference, as follows:
Mode 1: 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");
Method 2: Customize the build method
using System.Xml;// Header plus this namespace
XmlDocument xd = new XmlDocument();// Denote XML Document
XmlDeclaration xde;// Denote XML Declaration node: <?xml version='1.0'...?>
xde = xd.CreateXmlDeclaration("1.0", "GBK", null);// Parameter of the 2 Items are encoded
//standalone Defines whether the document can be processed without reading any other files , Default to no
xd.AppendChild(xde);//<?xml version="1.0" encoding="UTF-8" standalone="yes"?> End of generation
XmlElement xe = xd.CreateElement("Root");// Create 1 A Root Root element
xd.AppendChild(xe);//Root Root element creation complete
XmlNode root = xd.SelectSingleNode("Root");// Find <Root>
XmlElement xe1 = xd.CreateElement("Tree");// In <Root> Create elements under <Tree>
xe1.SetAttribute("id","1");// Specifies the property value of the property
xe1.InnerText = " Type 1";// Specify the attribute text node
root.AppendChild(xe1);// Complete child node <Tree>
xd.Save(Server.MapPath("xml.xml"));
For more readers interested in asp. net, please check the topics on this site: "Summary of asp. net Operation XML 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 programming.