Export the table to XML format directly using SQL statements

Source: Internet
Author: User

First, create a table:

Copy codeThe Code is as follows:
USE [ip]
GO
/***** Object: Table [dbo]. [SiteData] script Date: 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] (50) COLLATE Chinese_PRC_CI_AS not null,
[SiteAdd] [varchar] (100) 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

The table data is as follows:

 

The fields in the table are: Number, website name, website address, Baidu ranking, and Baidu.

Now we can export this table to XML format. The SQL statement is as follows:
Copy codeThe Code is as follows:
SELECT
1 AS Tag,
Null as Parent,
SiteName AS [site! 1! Website name],
SiteAdd AS [site! 1! Website address],
BaiduSort AS [site! 1! Baidu ranking],
BaiduShouLu AS [site! 1! Baidu indexing]
FROM SiteData order by BaiduShouLu FOR XML EXPLICIT

After running this statement, we can see the following in the running result of SQL Server2005:

We can see that the content of the XML node can be customized in SQL and distance. At this time, just click the output result, you can export a piece of code in XML format.

We will get:
Copy codeThe Code is as follows:
<Site name = "blog Park" website address = "http://www.jb51.net/" Baidu ranking = "20" Baidu indexed = "6020000"/>

The XML code we get now does not contain the XML header and the encoding format. If we want to be more foolish, we can add the following content directly in the background:
Copy codeThe Code is as follows:
// Content is the string we get after reading the database
Content = content. Replace ("<", "<"). Replace ("," \ ""). Replace (">", "> ");
// Customize the XML encoding format and parent node name
Content = "<? Xml version = \ "1.0 \" encoding = \ "UTF-8 \"?> <Start> "+ content +" </start>
// Output XML to the local path using
(StreamWriter sw = new StreamWriter (@ "E: \ SoftUpdate \ sql2xml. xml") {sw. Write (content );}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.