Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Server Valid XML

Posted on 2013-06-10
2
Medium Priority
?
462 Views
Last Modified: 2013-06-10
My following schema works and throws an error if an invalid schema is passed in

So if I use this
<root0MwljpDk4TE</root>

Open in new window


This error is thrown
Msg 6909, Level 16, State 1, Line 2
XML Validation: Text node is not allowed at this location, the type was defined with element only content or with simple content. Location: /*:root[1]

But I can't seem to get error handling to work.
If I use womething like this I don;t get the @@Error part returned. Just the initial XML Validation error
DECLARE @x XML(videoSchema)
        
SELECT  @x = '<root>0MwljpDk4TE=</root>'

IF @@error <> 0
   PRINT '@err is ' + ltrim(str(@@error)) + '.'

Open in new window



Heres my schema

CREATE XML SCHEMA COLLECTION videoSchema
    AS'<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="root" >
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="encString" />
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>'

DECLARE @x XML(videoSchema)
SELECT @x = '<root><encString>0MwljpDk4TE=</encString></root>'

SELECT @x

Open in new window

0
Comment
Question by:lrbrister
2 Comments
 
LVL 32

Accepted Solution

by:
Brendt Hess earned 2000 total points
ID: 39235835
I believe you need to move to the TRY... CATCH error handling for XML syntax errors.  These errors come from the XML engine, which is not perfectly integrated in SQL 2005.

Since all of your code should be in the TRY block, and all error handling in the CATCH block - and the @@error is not always correctly set in XML errors (as you discovered), I tend to work like this with XML coding in 2005

DECLARE @x XML(videoSchema)
DECLARE @stepFlag int

BEGIN TRY
    SET @stepFlag=1
    SELECT  @x = '<root0MwljpDk4TE=</root>'

    SET @stepFlag = 2
....

END TRY

BEGIN CATCH
    IF @stepFlag=1
    BEGIN
        PRINT 'XML Error at Step 1 - ' + @x
        PRINT '@errorvalue: ' + ltrim(str(@@error)) + '.'
    END

    IF @stepFlag=2
    BEGIN
         .....

Open in new window

0
 

Author Closing Comment

by:lrbrister
ID: 39235943
Got me started.

Thanks
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question