惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
人人都是产品经理
人人都是产品经理
月光博客
月光博客
V
V2EX
WordPress大学
WordPress大学
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
博客园 - 三生石上(FineUI控件)
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
V
Visual Studio Blog
宝玉的分享
宝玉的分享
雷峰网
雷峰网
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - Franky
博客园 - 聂微东
博客园 - 司徒正美
博客园 - 【当耐特】
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
阮一峰的网络日志
阮一峰的网络日志

博客园 - voiow

C#资源汇总 弹出窗口在屏幕中央 Server.CreateObject 的调用失败 - voiow - 博客园 SQL Server 2005表分区的具体实现方法 Server 2005之Integration Service简单实例 获取SQL Server数据库里表容量大小 自定义用户控件属性 - voiow - 博客园 使用asp下的adodb.stream 下载文件而不是打开 javascript以list方式封装xml - voiow - 博客园 将数据库存储的附件写到文件夹上面 - voiow - 博客园 如何用javascript拷贝当前页中被选中的文字? - voiow - 博客园 BizTalk使用发送端口组引起的问题解决方法 SQL Server日志清空方法 SQL SERVER 触发器实例 如何设计一个安全的WEBSERVICE 使用BizTalk Server常见问题处理 ASP.NET程序中常用的三十三种代码 浅谈反射与特性在接口系统中的应用(编码表转化) 使用window.createPopup();做的简单下拉菜单
Dynamic XML from SQL Server - voiow
voiow · 2010-01-03 · via 博客园 - voiow

"Efficiency in simplicity", that's what XML is all about. XML is great for information exchange because of its simple flat file structure and user defined tags. For any application to interact with another either an complex marshalling code would be required or a simple implementation of XML would suffice. MS SQL gives us the advantage of generating dynamic XML in our data queries itself. In this article we will see some of the common used SQL XML queries.

Introduction

In this article, we will see how to use normal relational data and prepare dynamic XML used for XML based applications. Though these features existed in the previous version of SQL Server they are highly enhanced in SQL Server 2005.

Our Data Table

Let us consider the famous customer table which looks usually looks as illustrated below:

cid cname cadd ctel
1 Name1 Address1 Tel1
2 Name2 Address2 Tel2
3 Name3 Address3 NULL
4 Name4 NULL NULL

Available XML queries in SQL

Below are common used XML queries that we are going to discuss in this article:

SELECT * FROM customer FOR XML RAW
SELECT * FROM customer FOR XML AUTO
SELECT * FROM customer FOR XML AUTO, ROOT('customers')
SELECT * FROM customer FOR XML AUTO, ELEMENTS, 

ROOT('customers')
SELECT * FROM customer FOR XML AUTO, ELEMENTS XSINIL, ROOT('customers')

The FOR XML Clause

The For XML clause does most of the work for us and it can be used in various ways. The basic syntax of the query is:

SELECT * FROM customer FOR XML [output mode], [display keyword]

RAW and AUTO output modes

These are two major used output modes which can further be customized by the display keywords to achieve most structures of XML derived from a particular table. The RAW mode takes each element as a row element and all the column values are taken as the attribute of that row element. On the other hand the AUTO mode outputs each element as the table name and the column values as attributes of these elements. Below is an example of the XML Raw and For XML Auto query:

SELECT * FROM customer FOR XML RAW

Output

<row cid="1" cname="Name1" cadd="Address1" ctel="Tel1"/>
<row cid="2" cname="Name2" cadd="Address2" ctel="Tel2"/>
<row cid="3" cname="Name3" cadd="Address3" />
<row cid="4" cname="Name4" />
SELECT * FROM customer FOR XML AUTO

Note: NULL values are omitted in both cases.

Display Keywords

There are many display keywords available and to cover all of them is out of the scope of this article, however I will cover the most used keywords.

1) ROOT

As we all know well formed XML documents must have a root node and subsequent instances of data should be under that one root node. To achieve this in our example we rewrite our query as:

SELECT * FROM customer FOR XML AUTO, ROOT('customers')

Output

<customers>
<customer cid="1" 

cname="Name1" cadd="Address1" ctel="Tel1"/>
<customer cid="2" 

cname="Name2" cadd="Address2" ctel="Tel2"/>
<customer cid="3" 

cname="Name3" cadd="Address3" />
<customer cid="4" 

cname="Name4" /> 
</customers> 

2) ELEMENTS

As we have seen above, the columns are simply attributes and not actually the node data. To get the data we use the ELEMENTS display keyword. This would break each cell in the relational table to an individual node.

SELECT * FROM customer FOR XML AUTO, ELEMENTS, 

ROOT('customers')

Output

<customers>
  <customer>
    <cid>1</cid>
    <cname>Name1</cname>
    <cadd>Address1</cadd>
    <ctel>Tel1</ctel>
  </customer>
  <customer>
    <cid>2</cid>
    <cname>Name2</cname>
    <cadd>Address2</cadd>
    <ctel>Tel2</ctel>
  </customer>
  <customer>
    <cid>3</cid>
    <cname>Name3</cname>
    <cadd>Address3</cadd>
  </customer>
  <customer>
    <cid>4</cid>
    <cname>Name4</cname>
  </customer>
</customers> 

3) ELEMENTS XSINIL

All thru out we see that the NULL values if the table are omitted and not accounted for. In many cases even the NULL values are required and important, hence we use the ELEMENTS XSINIL keyword to account for the same.

SELECT * FROM customer FOR XML AUTO,ELEMENTS 

XSINIL,ROOT('customers')

Output

<customers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <customer>
    <cid>1</cid>
    <cname>Name1</cname>
    <cadd>Address1</cadd>
    <ctel>Tel1</ctel>
  </customer>
  <customer>
    <cid>2</cid>
    <cname>Name2</cname>
    <cadd>Address2</cadd>
    <ctel>Tel2</ctel>
  </customer>
  <customer>
    <cid>3</cid>
    <cname>Name3</cname>
    <cadd>Address3</cadd>
    <ctel xsi:nil="true"/>
  </customer>
  <customer>
    <cid>4</cid>
    <cname>Name4</cname>
    <cadd xsi:nil="true"/>
    <ctel xsi:nil="true"/>
  </customer>
</customers> 

Summary

In this article you have seen how to generate XML from a relational table using SQL server 2005 and with the introduction of the xml datatype in SQL we are able to store and retrieve XML data easily. While the FOR XML clause gives us the power to tailor the XML to our needs.

http://dotnetslackers.com/articles/xml/dynamic_xml_from_sql_server.aspx