I have an XML like:
<?xml version="1.0" encoding="utf-16"?>
<ExportProjectDetailsMessage xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Project">
<CPProjectId>7665699f-6772-424c-8b7b-405b9220a8e7</CPProjectId>
</ExportProjectDetailsMessage>
I’m trying to get the CPProjectId as a Uniqueidentifier using:
DECLARE @myDoc xml
DECLARE @ProdID varchar(max)
SET @myDoc = '<ExportProjectDetailsMessage xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Project"><CPProjectId>7665699f-6772-424c-8b7b-405b9220a8e7</CPProjectId></ExportProjectDetailsMessage>'
SET @ProdID = @myDoc.value('(ExportProjectDetailsMessage/CPProjectId)[1]', 'varchar(max)' )
SELECT @ProdID
All i can receive is NULL =/
I’ve tried many combinations on @myDoc.value but no results =/
How can i retrieve the value from my XML ?
Thanks!
–EDIT:
Something that i noted, when i remove the namespace declaration from the XML it works fine!
The problem is that i need this namespaces! =/
You’re right the namespace is the issue. You’re query is looking for a node ExportProjectDetailsMessage but such a node doesn’t exist in your document, because there is a namespace declared as a default in your document. Since you can’t remove that (nor should you) you should include it in your XPATH query like so:
You may also want to consider not using varchar(max) but perhaps uniqueidentifier