DataContractSerializer encode \n
I am using the following code to serialize my object
DataContractSerializer ser = new DataContractSerializer(obj.GetType());
String text;
using (MemoryStream memoryStream = new MemoryStream())
{
ser.WriteObject(memoryStream, obj);
byte[] data = new byte[memoryStream.Length];
Array.Copy(memoryStream.GetBuffer(), data, data.Length);
text = Encoding.UTF8.GetString(data);
}
My object is serializing like this:
<Meta xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><Description>T开发者_如何学编程his is my new file
\n
\nMore Data</Description><Title>My Other Test Document</Title></Meta>
Notice that my \n was not escaped. Why is that? What is the best way to send \r\n through xml.
I searched and I dont see any articles about this. Am I missing some attribute in my serialize code?
Babel -- not at all, you're not missing any special attributes here. The \n is getting serialized. If it were being interpreted as a non-newline special character, you would see \n in the string, not \n itself. Are you not catching the newline on the client end, and have you verified it by spitting it out via an stdout call?
精彩评论