Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add xml encoding <?xml version="1.0" encoding="UTF-8"?> to xml Output in SQL Server

Tags:

Probably a duplicate of unanswered. SQL Server 2008 - Add XML Declaration to XML Output

Please let me know if this is possible. I read in some blogs

http://forums.asp.net/t/1455808.aspx/1

http://www.devnewsgroups.net/group/microsoft.public.sqlserver.xml/topic60022.aspx

But I couldn't understand why I can't do this.

like image 802
001priyank Avatar asked Jan 25 '12 12:01

001priyank


People also ask

How do you specify XML version and encoding in an XML document?

To avoid errors, you should specify the encoding used, or save your XML files as UTF-8. UTF-8 is the default character encoding for XML documents. Character encoding can be studied in our Character Set Tutorial. UTF-8 is also the default encoding for HTML5, CSS, JavaScript, PHP, and SQL.

How do I change the encoding of an XML file?

Another way to change the encoding of an XML document is to directly edit the encoding attribute of the document's XML declaration. Default encodings for existing and new XML and non-XML documents can be set in the Encoding section of the Options dialog.

What is UTF encoding in XML?

Encoding Types UTF stands for UCS Transformation Format, and UCS itself means Universal Character Set. The number 8 or 16 refers to the number of bits used to represent a character. They are either 8(1 to 4 bytes) or 16(2 or 4 bytes). For the documents without encoding information, UTF-8 is set by default.

What does <? XML version 1.0 ?> Mean?

version="1.0" means that this is the XML standard this file conforms to. encoding="utf-8" means that the file is encoded using the UTF-8 Unicode encoding.


2 Answers

You have to add it manually. SQL Server always stores xml internally as ucs-2 so it is impossible for SQL to generate it a utf-8 encoding header

See "Limitations of the xml Data Type" on MSDN

The XML declaration PI, for example, <?xml version='1.0'?>, is not preserved when storing XML data in an xml data type instance. This is by design. The XML declaration (<?xml ... ?>) and its attributes (version/encoding/stand-alone) are lost after data is converted to type xml. The XML declaration is treated as a directive to the XML parser. The XML data is stored internally as ucs-2.

like image 112
gbn Avatar answered Sep 30 '22 09:09

gbn


When I read this post I thought it was the "end of the line"... no solution... I almost gave up on the approach... but in fact there is a way to work around this limitation by converting the XML to varchar(max) and then appending the declaration to the beginning of the string. The following post shows how:

Using SQL Server "FOR XML": Convert Result Datatype to Text/varchar/string whatever?

A simple example would look something like this:

SELECT 'MY DATA' As MyColumn INTO #MyTable 
SELECT '<?xml version="1.0" encoding="UTF-8"?>' + 
CAST((SELECT MyColumn FROM #MyTable FOR XML PATH('')) AS VARCHAR(MAX)) AS XmlData
DROP TABLE #MyTable 

The Output:

<?xml version="1.0" encoding="UTF-8"?>
<MyColumn>MY DATA</MyColumn>
like image 36
MichaelBarce Avatar answered Sep 30 '22 10:09

MichaelBarce