Solved

SQL Server Valid XML

Posted on 2013-06-10
2
445 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 32

Accepted Solution

by:
Brendt Hess earned 500 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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.

615 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