I am trying to store some objects in an SQL Server Compact (SQL CE) database by serializing them with a SOAP formatter. Serializing seems to work just fine, but when I try to deserialize the object I get an error saying
There is an unclosed literal string. Line 53, position 72.
Furthermore, after restarting the application on attempting to fill the dataset I get the following error:
Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
All my columns (except the ID) allow for null values and are non-unique, so I have no idea where this comes from. Here is the code of my serializer:
public static class Serializer
{
static public string Serialize(AssessmentReport theObject)
{
MemoryStream mStream = new MemoryStream();
SoapFormatter formatter = new SoapFormatter();
formatter.Serialize(mStream, theObject);
byte[] buffer = mStream.ToArray();
mStream.Close();
string value = Encoding.UTF8.GetString(buffer);
return value;
}
static public AssessmentReport Deserialize(string value)
{
byte[] buffer = Encoding.UTF8.GetBytes(value);
MemoryStream mStream = new MemoryStream(buffer);
SoapFormatter formatter = new SoapFormatter();
mStream.Position = 0;
AssessmentReport theReport = (AssessmentReport)formatter.Deserialize(mStream);
mStream.Close();
return theReport;
}
}
Here is how I call the serializer (theReport is an instance of the object to be serialized):
examTableAdapter.UpdateAsmFile(Serializer.Serialize(theReport), examID);
And here is how I am calling the deserializing method:
string value = Convert.ToString(examTableAdapter.GetAsmFile(2));
AsmReport theReport = Serializer.Deserialize(value)
The field in the SQL Server Compact database where the string is saved is of type nvarchar with a limit of 3500.
I tried using a binary formatter, but when serializing it seems to always return an empty byte[] buffer. I really need deep serializing, that’s why the XML serializer is out of question.
Ok so this one I have been trying to figure out for more than two months now.
While I could not find a logical solution to the problem, it seems that changing the encoding to utf7 fixed the problem. I can’t think of a reason why this would be the issue, but it seems to be specific to my machine (I finally had the opportunity to run the code on another computer and it worked perfectly with utf8).