Solved

SQL Server Valid XML

Posted on 2013-06-10
2
429 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:
bhess1 earned 500 total points
Comment Utility
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
Comment Utility
Got me started.

Thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

744 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now