using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Xml;
public partial class SqlToXML: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
XmlDocument xml = new XmlDocument();
SqlConnection Conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["Northwind"].ConnectionString);
Conn.Open();
SqlCommand cmd = new SqlCommand("SELECT LastName, FirstName, Title FROM [Employees] FOR XML AUTO,root('root'), elements", Conn);
xml.Load(cmd.ExecuteXmlReader());
xml.Save(Server.MapPath("Employees.xml"));
}
}
輸出結果:
<root>
<LastName>Davolio</LastName>
<FirstName>Nancy</FirstName>
<Title>Sales Representative</Title>
</Employees>
<LastName>Fuller</LastName>
<FirstName>Andrew</FirstName>
<Title>Vice President, Sales</Title>
</Employees>
<LastName>Leverling</LastName>
<FirstName>Janet</FirstName>
<Title>Sales Representative</Title>
</Employees>
<LastName>Peacock</LastName>
<FirstName>Margaret</FirstName>
<Title>Sales Representative</Title>
</Employees>
<LastName>Buchanan</LastName>
<FirstName>Steven</FirstName>
<Title>Sales Manager</Title>
</Employees>
<LastName>Suyama</LastName>
<FirstName>Michael</FirstName>
<Title>Sales Representative</Title>
</Employees>
<LastName>King</LastName>
<FirstName>Robert</FirstName>
<Title>Sales Representative</Title>
</Employees>
<LastName>Callahan</LastName>
<FirstName>Laura</FirstName>
<Title>Inside Sales Coordinator</Title>
</Employees>
<LastName>Dodsworth</LastName>
<FirstName>Anne</FirstName>
<Title>Sales Representative</Title>
</Employees>
</root>
留言列表