0

After writing and reading an xml string to and from a stream, it ceases to be deserializable. The new string is clipped.

string XmlContent = getContentFromMyDataBase();
XmlSerializer xs = new XmlSerializer(typeof(MyObj));
MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms);
char[] ca = XmlContent.ToCharArray();      // still working up to this point.
ms.Position = 0;
sw.Write(ca);
StreamReader sr = new StreamReader(ms);
ms.Position = 0;
string XmlContentAgain = sr.ReadToEnd();
Console.WriteLine(XmlContentAgain);        // (outputstring is too short.)
MyObj theObj = (MyObj)xs.Deserialize(ms);  // Can't deserialize.

Any suggestions as to how to fix this or what is causing the problem? My only guess is that there is some form of encoding issue, but I wouldn't know how to go about finding/fixing it.

Additionally, myObj has a generic dictionary member, which typically isn't serializable, so I have stolen code from Paul Welter in order to serialize it.

2 Answers 2

2

Try flushing and disposing or even better simplify your code using a StringReader:

string xmlContent = getContentFromMyDataBase();
var xs = new XmlSerializer(typeof(MyObj));
using (var reader = new StringReader(xmlContent))
{
    var theObj = (MyObj)xs.Deserialize(reader);
}

Note: The getContentFromMyDataBase method also suggests that you are storing XML in your database that you are deserializing back to an object. Don't.

Sign up to request clarification or add additional context in comments.

14 Comments

Why wouldn't you store XML in a database?
Because a SQL database is relational. XML is hierarchical. They are not compatible. Database is for storing data, not for storing representation of the data (which is XML).
XML is text. Databases are very good at storing text. It's entirely appropriate to store XML in a database. Your distinction between "data" and "representation of the data" is arbitrary and meaningless.
@Mark, I have to disagree with you. XML itself can be seen as a database for storing data, but storing database inside another database seems wrong to me.
The xml is stored in the database, as text.
|
0

You need to Flush or Close (closing implicitly flushes) the StreamWriter, or you cannot be sure it is done writing to the underlying stream. This is because it is doing some internal buffering.

Try this:

using(StreamWriter sw = new StreamWriter(ms)) 
{
    char[] ca = XmlContent.ToCharArray();      // still working up to this point.
    ms.Position = 0;
    sw.Write(ca);
}
StreamReader sr = new StreamReader(ms);
ms.Position = 0;
string XmlContentAgain = sr.ReadToEnd();

2 Comments

This doesn't work. The stream becomes inaccessible after the using block. Moving the code from outside to inside results in the same problems as before.
That is because disposing StreamWriter also disposes the underlying object. You can get the buffer from the stream, or you can call Flush on your writer instead of disposing it.

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.