I've got a problem with SQLParameter when I try to use one to store into a column of XML type. I am creating the parameter of SqlDbType.Xml. It appears that any XML Elements that are only white space are converted to closed elements.
This is the same behaviour as
SELECT CAST('<Cust><Fname>Andrew</Fname><Lname> </Lname></Cust>' as XML)
VS
SELECT CONVERT(xml, '<Cust><Fname>Andrew</Fname><Lname> </Lname></Cust>', 1)
I'm surprised to see that as the default behaviour within SQLParameter. Is there some way to make a SQLParameter of SqlDbType.Xml care about its white space? As a shot in the dark i tried setting the Precision to 1, but that didn't help it...
The code below ends up with this in the database
<Cust><Fname>Andrew</Fname><Lname /></Cust>
As you can see it strips out the whitespace XML element
// CREATE TABLE T(c1 int primary key, c2 xml)
static void Main(string[] args) {
using (var Connection = new System.Data.SqlClient.SqlConnection("Data Source=localhost;Initial Catalog=Test;Integrated Security=SSPI;"))
{
Connection.Open();
using (var Command = Connection.CreateCommand())
{
Command.CommandText = "INSERT INTO T VALUES (1, @s)";
var XmlParameter = Command.Parameters.Add("s", System.Data.SqlDbType.Xml);
XmlParameter.Value = "<Cust><Fname>Andrew</Fname><Lname> </Lname></Cust>";
Command.ExecuteNonQuery();
}
Connection.Close();
}
Does anyone know of a way to solve it that isn't parsing the parameter in as binary and the converting it as part of the insert/update command?
Command.CommandText = "INSERT INTO T VALUES (1, CONVERT(xml, @s, 1))";
Any help or feedback would be greatly appreciated.