8

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"

Any ideas?

3
  • 1
    did you manage to find the solution? Commented Jun 28, 2016 at 11:22
  • 2
    Nope, I am still manually changing encoding="utf-16" Commented Jun 29, 2016 at 16:09
  • 1
    I'm also having the same problem. Is there a way to support this in EF without have to revert back to ADO.NET? Commented Nov 24, 2017 at 10:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.