I have the following c# class
public class Data
{
public string A {get;set;}
public string B {get;set;}
public string C {get;set;}
}
I create an instance of this in my code:
var d = new Data { A = "abc", B = string.Empty, C = null};
I need to convert this to XML and pass it to a sproc in SQL Server 2008.
I do this using:
var xml = new XElement("Data",
new XElement("A", d.A),
new XElement("B", d.B),
new XElement("C", d.C));
The resultant XML in the sproc is:
<Data>
<A>abc</A>
<B></B>
<C />
</Data>
So, there's a difference between an empty string an a null value.
Then in SQL, I'm using the following syntax to read the XML:
INSERT INTO #myTable
SELECT
nref.value('(A/text())[1]', 'uniqueidentifier') [A],
nref.value('(B/text())[1]', 'uniqueidentifier') [B],
nref.value('(C/text())[1]', 'uniqueidentifier') [C],
FROM @DataXml.nodes('/Data') AS R(nref);
But this is providing me with B and C both as NULL, where B should be empty string.
How can I adapt this to ensure that nulls remain as nulls and empty strings remain as empty strings?
Thanks