I'm trying to create a database that will act as a repository for a series XML files that are being generated.
In my SQL Server database, I have a table similar to the following to store the XML file:
CREATE TABLE [dbo].[MyTable]
(
[InternalID] INT NOT NULL IDENTITY,
[ExternalID] INT NOT NULL,
[XmlData] XML NOT NULL
)
Each of the XML files is encoded in US-ASCII with the following header:
<?xml version="1.0" encoding="us-ascii"?>
I've got a stored procedure created to insert the file into the table, and I'm hitting it with .NET using the following:
int externalKey = someint;
string myXML = File.ReadAllText(xmlFilePath);
using (SqlCommand myCommand = new SqlCommand ("My_Stored_Proc", myConnection)) {
myCommand.CommandType = SqlCommandType.StoredProcedure;
myCommand.Parameters.Add(new SqlParameter ("@ExternalID", externalID));
myCommand.Parameters.Add(new SqlParameter ("@XmlData", myXML));
myCommand.ExecuteNonQuery();
}
When my code tries to run, it encounters the following SqlException
when trying to execute the non-query:
XML parsing: line 1, character 41, unable to switch the encoding
If I modify the string before I execute by replacing the encoding="us-ascii"
with one that uses UTF-8, it goes in fine. But it would be preferred to not have the source modified in the database.