通过SQL语句直接把表导出为XML格式
前端之家收集整理的这篇文章主要介绍了
通过SQL语句直接把表导出为XML格式,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
首先建立一张表: <div class="codetitle"><a style="CURSOR: pointer" data="66241" class="copybut" id="copybut66241" onclick="doCopy('code66241')"> 代码如下:
<div class="codebody" id="code66241">
USE [ip]
GO
/
** 对象: Table [dbo].[SiteData] 脚本日期: 09/11/2010 17:41:11
**/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[SiteData](
[id] [int] IDENTITY(1,1) NOT NULL,
[SiteName]
varchar COLLATE Chinese_PRC_CI_AS NOT NULL,
[SiteAdd]
varchar COLLATE Chinese_PRC_CI_AS NOT NULL,
[BaiduSort] [int] NOT NULL CONSTRAINT [DF_SiteData_BaiduSort] DEFAULT ((0)),
[BaiduShouLu] [int] NOT NULL CONSTRAINT [DF_SiteData_BaiduShouLu] DEFAULT ((0)),
CONSTRAINT [PK_SiteData] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF,IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] GO
SET ANSI_PADDING OFF
SELECT
1 AS Tag,
NULL AS Parent,
SiteName AS [site!1!网站
名称],
SiteAdd AS [site!1!网站地址],
BaiduSort AS [site!1!
百度排名],
BaiduShouLu AS [site!1!
百度收录]
FROM SiteData order by BaiduShouLu FOR XML EXPLICIT
//content 是我们读取
数据库后得到的string字符串
content = content.Replace("<","<").Replace(""","\"").Replace(">",">");
//
自定义XML编码格式和父节点
名称 content = "
" + content + " //
输出XML到本地路径using
(StreamWriter sw = new StreamWriter(@"E:\SoftUpdate\
sql2xml.xml")) { sw.Write(content); }