I am using Entity Framework version 6.0.0 in a C# Project that uses .NET Framework 4.5. I have a class Filing who has a SQL Server column XmlContent which is xml datatype. I have Database-first Entity Framework I am using primarily to save rows to the Filing table (I currently don't read those rows back, if that makes a difference)
I've read these posts regarding a "string" backing property to save to an XML column using Entity Framework, as described here and here
My XML declaration looks like this
<?xml version="1.0" encoding="UTF-8"?>
....
And my Entity Framework auto-generated (database-first) looks like this:
public partial class Filing
{
// ...
public string XmlContent { get; set; }
// ...
}
With my custom partial class like this (saving to the string property...):
public partial class Filing
{
[NotMapped]
public XmlDocument XmlDocument
{
get
{
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(this.XmlContent);
return xmlDocument;
}
set
{
this.XmlContent = value.OuterXml;
}
}
// ...
}
When I do this:
eFileEntities.Filings.Add(filingWithUtf8XmlString);
eFileEntities.SaveChanges();
I get an exception, whose InnerException (eventually), reads:
"XML Parsing : line 1 , character 38, unable to switch the encoding."
I've read these posts regarding the error I'm getting, "unable to switch the encoding"
- https://stackoverflow.com/a/8998183/1175496
- https://stackoverflow.com/a/1091209/751158
- These answers suggest using a
SqlXmldatatype, so there is no need to bother about encodings. But I don't know how to do that in the context of Entity Framework
- These answers suggest using a
- https://stackoverflow.com/a/385756/1175496
- https://stackoverflow.com/a/1564803/1175496
- If I change XML declaration to use
encoding="utf-16"as these answers suggests, EntityFramework cansaveChanges()with no errors, but I want to avoid (if possible):- Changing encodings
- Stripping the declaration (even though I understand SQL Server doesn't store the declaration anyway)
- If I change XML declaration to use
Any ideas?
encoding="utf-16"