I have written a stored procedure that returns my rows as XML with the following syntax:
FOR XML PATH ('customer'), ROOT ('customers'), TYPE
Running the query from the stored procedure in SSMS produces output that when clicked, looks just fine.
However, calling that stored procedure from C# gives me XML that I assume has double quotes that are escaped, thus:
<customers><customer id=\"123456\" firstName=\"ABE\" lastName=\"LINCOLN\" dob=\"02/12/1809\">
My C# code looks like this:
public string GetCustomerXML()
{
string xml = string.Empty;
XmlDocument xmlDoc = new XmlDocument();
using (SqlConnection myConnection = new SqlConnection(connectionString))
{
myConnection.Open();
using (SqlCommand cmd = new SqlCommand("GetData_CreateXML", myConnection))
{
cmd.CommandType = CommandType.StoredProcedure;
using (XmlReader reader = cmd.ExecuteXmlReader())
{
while (reader.Read())
{
xmlDoc.Load(reader);
xml = xmlDoc.OuterXml.ToString();
}
}
}
}
return xml;
}
What am I getting wrong here?
Thanks in advance.
I suspect you’re just examining the string in the debugger, which generally escapes the string as if you wanted to write it as a C# string literal.
Try logging the string (e.g. something as simple as writing it to the console) and I suspect you’ll find those backslashes simply don’t exist.