0

I want to read the value in xml returned by a WCF service which gets called by a stored procedure. The code below returns a null:

declare @xmlValue  varchar(1000)
declare @responseString varchar(100)
declare @idoc int

set @xmlValue = '<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">220</string>'

EXEC sp_xml_preparedocument @idoc OUTPUT, @xmlValue

SELECT *
FROM OPENXML (@idoc, '/*',3)
      WITH (string  varchar(100))

EXEC sp_xml_removedocument @idoc

1 Answer 1

1

Of course - the XML has a XML namespace that you're ignoring - you need to pay attention to that!

I would also recommend to use the native XQuery support in SQL Server.

Try this:

DECLARE @xmlvalue XML
SET @xmlValue = '<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">220</string>'

-- define the XML namespace
;WITH XMLNAMESPACES('http://schemas.microsoft.com/2003/10/Serialization/' AS ns)
SELECT
    @xmlValue.value('(ns:string)[1]', 'int')  -- and **use** the XML namespace!
Sign up to request clarification or add additional context in comments.

2 Comments

thanks marc_s, i forgot to mention that my sqlserver is old...do you know how to do it in sql2000 ?
@AndrisMudiayi: sorry, no, I don't know how to do this in SQL Server 2000 - this is just way to old now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.