I have serialized an object into a XML column in my SQL Server 2008 database.
The xml looks like this:
<SimpleUserData xmlns="http://schemas.datacontract.org/2004/07/MyProject.CustomFx.Web" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" z:Id="1" z:Type="MyProject.CustomFx.Web.SimpleUserData" z:Assembly="MyProject.CustomFx.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<BatchDescription z:Id="2">I want this value</BatchDescription>
<BatchID>9c437c08-0f28-4c77-9a50-49a7a4e8930a</BatchID>
<BatchTemplateID>And this value too</BatchTemplateID>
</SimpleUserData>
I want to write a query that shows the values inside the elements BatchDescription and BatchTemplateID.
My query as it stands now looks like this:
declare @xmlobj as xml = '<SimpleUserData xmlns="http://schemas.datacontract.org/2004/07/MyProject.CustomFx.Web" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" z:Id="1" z:Type="MyProject.CustomFx.Web.SimpleUserData" z:Assembly="MyProject.CustomFx.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<BatchDescription z:Id="2">I want this value</BatchDescription>
<BatchID>9c437c08-0f28-4c77-9a50-49a7a4e8930a</BatchID>
<BatchTemplateID>And this value too</BatchTemplateID>
</SimpleUserData>';
select
@xmlobj.query('
declare namespace x="http://schemas.datacontract.org/2004/07/MyProject.CustomFx.Web";
(/x:SimpleUserData[1]/x:BatchDescription[1])
') as value1
,@xmlobj.query('
declare namespace x="http://schemas.datacontract.org/2004/07/MyProject.CustomFx.Web";
(/x:SimpleUserData[1]/x:BatchTemplateID[1])
') as value2;
This comes close; it returns the entire tag and the value inside it.
How can I get just the values (i.e., "I want this value" and "And this value too")