- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我有以下两个功能:
public static string Serialize(object obj)
{
DataContractSerializer serializer = new DataContractSerializer(obj.GetType());
MemoryStream memoryStream = new MemoryStream();
serializer.WriteObject(memoryStream, obj);
return Encoding.UTF8.GetString(memoryStream.GetBuffer());
}
public static object Deserialize(string xml, Type toType)
{
MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(xml));
// memoryStream.Position = 0L;
XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(memoryStream, Encoding.UTF8, new XmlDictionaryReaderQuotas(), null);
DataContractSerializer dataContractSerializer = new DataContractSerializer(toType);
return dataContractSerializer.ReadObject(reader);
}
第一个似乎可以很好地将对象序列化为 xml 字符串。 XML 看起来有效,没有损坏的标签,开头或结尾没有空格等。现在第二个函数不想将我的 xml 字符串反序列化回对象。在最后一行我得到:
There was an error deserializing the object of type [MY OBJECT TYPE HERE]. The data at the root level is invalid. Line 1, position 1.
我做错了什么?我试过几次重写 Deserialize 函数,似乎总是同一种错误。谢谢!
哦,这就是我调用这 2 个函数的方式:
SomeObject so = new SomeObject();
string temp = SerializationManager.Serialize(so);
so = (SomeObject)SerializationManager.Deserialize(temp, typeof(SomeObject));
最佳答案
这是我一直以来的做法:
public static string Serialize(object obj) {
using(MemoryStream memoryStream = new MemoryStream())
using(StreamReader reader = new StreamReader(memoryStream)) {
DataContractSerializer serializer = new DataContractSerializer(obj.GetType());
serializer.WriteObject(memoryStream, obj);
memoryStream.Position = 0;
return reader.ReadToEnd();
}
}
public static object Deserialize(string xml, Type toType) {
using(Stream stream = new MemoryStream()) {
byte[] data = System.Text.Encoding.UTF8.GetBytes(xml);
stream.Write(data, 0, data.Length);
stream.Position = 0;
DataContractSerializer deserializer = new DataContractSerializer(toType);
return deserializer.ReadObject(stream);
}
}
关于c# - 使用DataContractSerializer序列化,反序列化不回来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5010191/
我对 Objective C 和 iOS 开发总体来说是新手。我正在尝试创建一个应用程序,它会发出 http 请求并在标签上显示内容。 当我开始测试时,我注意到标签是空白的,尽管我的日志显示我已经恢复
我实现了一个 MVP 应用程序。后退按钮工作正常。我想以编程方式回到以前的地方。我该怎么做? 我正在寻找类似的东西: clientFactory.getPlaceController().goBack
我想从 mySubView 制作后退按钮。我有带有按钮的付款ViewController,这个按钮使view.addSubview(WkWebView)。所以我可以打开 WkWebView 但我不可能
我有一个 master 分支,里面有很多不会发布的特性。我被要求从 master 中删除这些功能并创建一个包含它们的新分支,以便我们稍后可以 merge 回 master。 我采取的步骤是: 在 ma
如何禁用页面上的所有 onclick 事件,绑定(bind)我的自定义函数,并在执行后启用所有之前的事件? 我正在构建一个小书签,它应该适用于任何已加载的页面,并且我正在使用 jQuery 来处理我的
我正在尝试从 Spotify api 接收 token 。不幸的是,我一直收到 415。你能帮帮我,让我知道我做错了什么吗? const axios = require('axios'); const
我有一个提供上下文的函数: def buildContext(s:String)(request:RequestHeader):Future[Granite.Context] = { ....
我有一个列表,其中包含几个不同形状的 numpy 数组。我想将这个数组列表 reshape 为一个 numpy 向量,然后更改向量中的每个元素,然后将其 reshape 回原始数组列表。 例如: 输入
我目前有这个工作 fiddle - http://jsfiddle.net/B8Abd/ 我想在函数中使用 jquerys 淡出然后淡入。目前的代码是这样的: function chan
我有 2 个分支,一个是 main 分支,另一个是我正在开发一个 parallel 版本。 A --> B --> C (master) \ -> E --> F (parallel) pa
我试图从我的“ super 项目”中的文件目录中创建一个“子项目”以与其他人一起工作,但我一直在努力使其在 git subtree 中工作。 理想情况下,其他人可以在子项目上工作,然后我从上游更改中提
有没有其他人看到这个? 我正在使用带有回形针的 rails 3,当我上传 .doc 时,它的应用程序/msword 效果很好,但是当我上传 .docx 时,content_type 被保存为应用程序/
我是一名优秀的程序员,十分优秀!