DataContractSerializer 编码 \n

发布于 2024-10-01 17:32:09 字数 743 浏览 12 评论 0原文

我正在使用以下代码来序列化我的对象

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&#xD;\n&#xD;\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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贩梦商人 2024-10-08 17:32:09

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文