DataContractSerializer 编码 \n
我正在使用以下代码来序列化我的对象
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);
}
我的对象是这样序列化的:
<Meta xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><Description>This is my new file
\n
\nMore Data</Description><Title>My Other Test Document</Title></Meta>
请注意,我的 \n 没有转义。这是为什么?通过 xml 发送 \r\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>This 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(1)
Babel——一点也不,你不会错过任何特殊属性。 \n 正在 被序列化。如果它被解释为非换行符特殊字符,您将在字符串中看到 \n,而不是 \n 本身。您是否没有在客户端捕获换行符,并且是否通过标准输出调用将其吐出来验证它?
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?