Test a few XML questions

Source: Internet
Author: User

When using SQL Server, it is unavoidable to deal with XML parameters, XML most of the time it is convenient for our program, but there are times when variable assignment does not pass. (Of course, if you have XML that doesn't pass the check of software like the XML Spy, that's not the scope of it.)

The example of sharing today is very simple, just test a few examples

DECLARE @xXML--1SELECT @x ='<a>1</a>'--2SELECT @x ='<?xml version= "1.0" encoding= "Utf-8"?><a>1</a>'--3SELECT @x =N'<?xml version= "1.0" encoding= "Utf-8"?><a>1</a>'--4SELECT @x ='<?xml version= "1.0" encoding= "Utf-8"?><a> a person </a>'--5SELECT @x ='<?xml version= "1.0" encoding= "GBK"?><a> single dog Wang </a>'

Example 1:

We usually see the most examples of compiling through stress-free. Variable assignment through, then query, parse, with you ~

Example 2:

Compile is also passed, it seems that this is the most likely to cause misunderstanding, I have always thought that the assignment in SQL Server is not supported with

<?xml version= "1.0" encoding= "Utf-8"?>

This head, so usually with coder said if there is such a mistake, the head is removed (it will be good, but the reason is wrong (⊙﹏⊙) b). In fact, the XML type is supported, only when we call the stored procedure or the parameters within the statement when the application of the scene problem. SQL Server means I don't carry this pot.

Example 3:

This example compiles with a problem, and the compiler throws

Msg 9402, Level 16, State 1, line 8th
XML parsing: Line 1, character 38, unable to toggle encoding

However, the difference between example 3 and Example 2 is that the assignment of Example 3 uses Unicode encoding and Example 2 does not do so, so example 3 instantly knelt ╮ (╯_╰) ╭. So we usually found that the database of the error is because of the use of this way, so I have been fooled _ (: З"∠) _. So it's not unsupported, just the way we call it.

Example 4:

Msg 9420, Level 16, State 1, line 9th
XML parsing: Line 2, character 5, illegal XML character

Ah ~ and error again ~ This time is illegal XML characters, it seems that the code is UTF-8 this does not support Chinese. So sometimes these details don't pay attention .../(ㄒoㄒ)/~~

Example 5:

Compile smoothly through, this time the inside of the code into GBK code, you can support Chinese. Of course the compilation is also completely no problem ROM.

Add another example

SELECT @x =
' <?xml version= ' 1.0 "encoding=" GBK "?>
<a> characters Dragon _ (: З"∠) _</a>

Is OK, some traditional Chinese characters in the GBK of the font can also support, and generally do not necessarily need to tangle with this problem. Unless some special symbols, it is difficult to say Oh da

Finally, encoding= "Utf-8" and encoding= "UTF-8" are equivalent and are not case-sensitive here. Attention is here ...

Share this little example today ~ If anything is wrong, be sure to correct it.

Test a few XML questions

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.