一、模式raw

select top 5
EmployeeID,NationalIDNumber,Title,BirthDate
from HumanResources.Employee for xml raw
go

输出结果:

<row EmployeeID="1" NationalIDNumber="14417807" Title="Production Technician - WC60" BirthDate="1972-05-15T00:00:00" />
<row EmployeeID="2" NationalIDNumber="253022876" Title="Marketing Assistant" BirthDate="1977-06-03T00:00:00" />
<row EmployeeID="3" NationalIDNumber="509647174" Title="Engineering Manager" BirthDate="1964-12-13T00:00:00" />
<row EmployeeID="4" NationalIDNumber="112457891" Title="Senior Tool Designer" BirthDate="1965-01-23T00:00:00" />
<row EmployeeID="5" NationalIDNumber="480168528" Title="Tool Designer" BirthDate="1949-08-29T00:00:00" />

二、模式auto

select top 5
EmployeeID,NationalIDNumber,Title,BirthDate
from HumanResources.Employee as Emloyee for xml auto

输出结果:

<Emloyee EmployeeID="1" NationalIDNumber="14417807" Title="Production Technician - WC60" BirthDate="1972-05-15T00:00:00" />
<Emloyee EmployeeID="2" NationalIDNumber="253022876" Title="Marketing Assistant" BirthDate="1977-06-03T00:00:00" />
<Emloyee EmployeeID="3" NationalIDNumber="509647174" Title="Engineering Manager" BirthDate="1964-12-13T00:00:00" />
<Emloyee EmployeeID="4" NationalIDNumber="112457891" Title="Senior Tool Designer" BirthDate="1965-01-23T00:00:00" />
<Emloyee EmployeeID="5" NationalIDNumber="480168528" Title="Tool Designer" BirthDate="1949-08-29T00:00:00" />

三、模式explicit

select 1 as Tag,NULL as Parent,
SalesOrderID as [Invoice!1!InvoiceNo],
OrderDate as [Invoice!1!Date!Element]
from Sales.SalesOrderHeader where SalesOrderID<=43666
for xml explicit,root('Sale')

--通用表的组成

ElementName!TagNumber!AttributeName!Directive

ElementName:表示列里面的元素名称,如:Invoice

TagNumber:选择性项目,指定标识列的数值,相同元素的标识值一样

Directive:选择性项目,指明是子元素或是xml特殊格式

输出结果:

<Sale>
<Invoice InvoiceNo="43659">
<Date>2001-07-01T00:00:00</Date>
</Invoice>
<Invoice InvoiceNo="43660">
<Date>2001-07-01T00:00:00</Date>
</Invoice>
<Invoice InvoiceNo="43661">
<Date>2001-07-01T00:00:00</Date>
</Invoice>
<Invoice InvoiceNo="43662">
<Date>2001-07-01T00:00:00</Date>
</Invoice>
<Invoice InvoiceNo="43663">
<Date>2001-07-01T00:00:00</Date>
</Invoice>
<Invoice InvoiceNo="43664">
<Date>2001-07-01T00:00:00</Date>
</Invoice>
<Invoice InvoiceNo="43665">
<Date>2001-07-01T00:00:00</Date>
</Invoice>
<Invoice InvoiceNo="43666">
<Date>2001-07-01T00:00:00</Date>
</Invoice>
</Sale>

四、模式path

select top 5
EmployeeID as '@员工编号',--前面加一个这个表示是属性
Title as '称呼',BirthDate as '出生日期'
from HumanResources.Employee for xml path('员工'),root('员工列表')

输出结果:

<员工列表>
<员工 员工编号="1">
<称呼>Production Technician - WC60</称呼>
<出生日期>1972-05-15T00:00:00</出生日期>
</员工>
<员工 员工编号="2">
<称呼>Marketing Assistant</称呼>
<出生日期>1977-06-03T00:00:00</出生日期>
</员工>
<员工 员工编号="3">
<称呼>Engineering Manager</称呼>
<出生日期>1964-12-13T00:00:00</出生日期>
</员工>
<员工 员工编号="4">
<称呼>Senior Tool Designer</称呼>
<出生日期>1965-01-23T00:00:00</出生日期>
</员工>
<员工 员工编号="5">
<称呼>Tool Designer</称呼>
<出生日期>1949-08-29T00:00:00</出生日期>
</员工>
</员工列表>

<员工列表>
<员工信息 员工编号="1">
<员工>
<名>Gustavo</名>
<姓>Achong</姓>
</员工>
</员工信息>
<员工信息 员工编号="2">
<员工>
<名>Catherine</名>
<姓>Abel</姓>
</员工>
</员工信息>
<员工信息 员工编号="3">
<员工>
<名>Kim</名>
<姓>Abercrombie</姓>
</员工>
</员工信息>
<员工信息 员工编号="4">
<员工>
<名>Humberto</名>
<姓>Acevedo</姓>
</员工>
</员工信息>
<员工信息 员工编号="5">
<员工>
<名>Pilar</名>
<姓>Ackerman</姓>
</员工>
</员工信息>
</员工列表>

输出结果:

<员工列表>
<员工信息 员工编号="1">
<员工>
<名>Gustavo</名>
<姓>Achong</姓>
</员工>
</员工信息>
<员工信息 员工编号="2">
<员工>
<名>Catherine</名>
<姓>Abel</姓>
</员工>
</员工信息>
<员工信息 员工编号="3">
<员工>
<名>Kim</名>
<姓>Abercrombie</姓>
</员工>
</员工信息>
<员工信息 员工编号="4">
<员工>
<名>Humberto</名>
<姓>Acevedo</姓>
</员工>
</员工信息>
<员工信息 员工编号="5">
<员工>
<名>Pilar</名>
<姓>Ackerman</姓>
</员工>
</员工信息>
</员工列表>

架构的处理

select top 5
EmployeeID,NationalIDNumber,Title,BirthDate
from HumanResources.Employee as Emloyee for xml auto,xmlschema--(适用于for xml raw,for auto,for explicit)

select top 5
EmployeeID,NationalIDNumber,Title,BirthDate
from HumanResources.Employee as Emloyee for xml auto,xmldata--(适用于for raw,for auto,不能在explicit模式中指定)

null值的处理

select ProductID '产品代码',Name '名称',Color '颜色'
from Production.Product Product
where ProductID in(1,999) for xml auto,elements--默认的是值为null的会自动删除的
go

输出结果:

<Product>
<产品代码>1</产品代码>
<名称>Adjustable Race</名称>
</Product>
<Product>
<产品代码>999</产品代码>
<名称>Road-750 Black, 52</名称>
<颜色>Black</颜色>
</Product>

select ProductID '产品代码',Name '名称',Color '颜色'
from Production.Product Product
where ProductID in(1,999) for xml auto,elements xsinil
go

输出结果:

<Product xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<产品代码>1</产品代码>
<名称>Adjustable Race</名称>
<颜色 xsi:nil="true" />
</Product>
<Product xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<产品代码>999</产品代码>
<名称>Road-750 Black, 52</名称>
<颜色>Black</颜色>
</Product>

posted on 2012-11-19 14:06  andylau168  阅读(333)  评论(0编辑  收藏  举报