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>
- <Employees>
  <LastName>Davolio</LastName>
  <FirstName>Nancy</FirstName>
  <Title>Sales Representative</Title>
  </Employees>
- <Employees>
  <LastName>Fuller</LastName>
  <FirstName>Andrew</FirstName>
  <Title>Vice President, Sales</Title>
  </Employees>
- <Employees>
  <LastName>Leverling</LastName>
  <FirstName>Janet</FirstName>
  <Title>Sales Representative</Title>
  </Employees>
- <Employees>
  <LastName>Peacock</LastName>
  <FirstName>Margaret</FirstName>
  <Title>Sales Representative</Title>
  </Employees>
- <Employees>
  <LastName>Buchanan</LastName>
  <FirstName>Steven</FirstName>
  <Title>Sales Manager</Title>
  </Employees>
- <Employees>
  <LastName>Suyama</LastName>
  <FirstName>Michael</FirstName>
  <Title>Sales Representative</Title>
  </Employees>
- <Employees>
  <LastName>King</LastName>
  <FirstName>Robert</FirstName>
  <Title>Sales Representative</Title>
  </Employees>
- <Employees>
  <LastName>Callahan</LastName>
  <FirstName>Laura</FirstName>
  <Title>Inside Sales Coordinator</Title>
  </Employees>
- <Employees>
  <LastName>Dodsworth</LastName>
  <FirstName>Anne</FirstName>
  <Title>Sales Representative</Title>
  </Employees>
  </root>

 

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 lansilote 的頭像
    lansilote

    慢慢來比較快

    lansilote 發表在 痞客邦 留言(0) 人氣()