asp.net simple way to generate XML files-practical tips

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.