为什么 XML 反序列化不会触发 OnDeserialization?
我有一个问题,在三个小时的大部分时间里我一直在努力解决这个问题。我几乎可以肯定我错过了一些非常明显的东西...
我有一个简单的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<WeightStore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Records>
<Record actual="150" date="2010-05-01T00:00:00" />
<Record actual="155" date="2010-05-02T00:00:00" />
</Records>
</WeightStore>
我有一个简单的类结构:
[Serializable]
public class Record
{
[XmlAttribute("actual")] public double weight { get; set; }
[XmlAttribute("date")] public DateTime date { get; set; }
[XmlIgnore] public double trend { get; set; }
}
[Serializable]
[XmlRoot("WeightStore")]
public class SimpleWeightStore
{
[XmlArrayAttribute("Records")]
private List<Record> records = new List<Record>();
public List<Record> Records { get { return records; } }
[OnDeserialized()]
public void OnDeserialized_Method(StreamingContext context)
{
// This code never gets called
Console.WriteLine("OnDeserialized");
}
}
我在调用代码和类文件中使用这些:
using System.Xml.Serialization;
using System.Runtime.Serialization;
我有一些调用代码
SimpleWeightStore weight_store_reload = new SimpleWeightStore();
TextReader reader = new StringReader(xml);
XmlSerializer deserializer = new XmlSerializer(weight_store.GetType());
weight_store_reload = (SimpleWeightStore)deserializer.Deserialize(reader);
:问题是我期望 OnDeserialized_Method 被调用,但事实并非如此。
我怀疑这可能与它是 XML 反序列化而不是运行时反序列化这一事实有关,也许我使用了错误的属性名称,但我无法找出它可能是什么。
各位有什么想法吗?
I have a problem which I have been bashing my head against for the better part of three hours. I am almost certain that I've missed something blindingly obvious...
I have a simple XML file:
<?xml version="1.0" encoding="utf-8"?>
<WeightStore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Records>
<Record actual="150" date="2010-05-01T00:00:00" />
<Record actual="155" date="2010-05-02T00:00:00" />
</Records>
</WeightStore>
I have a simple class structure:
[Serializable]
public class Record
{
[XmlAttribute("actual")] public double weight { get; set; }
[XmlAttribute("date")] public DateTime date { get; set; }
[XmlIgnore] public double trend { get; set; }
}
[Serializable]
[XmlRoot("WeightStore")]
public class SimpleWeightStore
{
[XmlArrayAttribute("Records")]
private List<Record> records = new List<Record>();
public List<Record> Records { get { return records; } }
[OnDeserialized()]
public void OnDeserialized_Method(StreamingContext context)
{
// This code never gets called
Console.WriteLine("OnDeserialized");
}
}
I am using these in both calling code and in the class files:
using System.Xml.Serialization;
using System.Runtime.Serialization;
I have some calling code:
SimpleWeightStore weight_store_reload = new SimpleWeightStore();
TextReader reader = new StringReader(xml);
XmlSerializer deserializer = new XmlSerializer(weight_store.GetType());
weight_store_reload = (SimpleWeightStore)deserializer.Deserialize(reader);
The problem is that I am expecting OnDeserialized_Method to get called, and it isn't.
I suspect it might have something to do with the fact that it's XML deserialization rather than Runtime deserialization, and perhaps I am using the wrong attribute name, but I can't find out what it might be.
Any ideas, folks?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
XML 反序列化没有与
OnDeserialized
等效的方法。请参阅此帖子了解解决方法:如何做你知道什么时候通过 XML 序列化加载了吗?
There's no equivalent of
OnDeserialized
for XML deserialization.See this post for workarounds: How do you find out when you've been loaded via XML Serialization?
以优雅的方式做到这一点的唯一方法是手动实现 IXmlSerialized ,但这不很有趣。简单地;
XmlSerializer
不支持序列化回调。但有时,您可以切换到 DataContractSerializer,它仍然提供 xml 功能,但支持序列化回调。不幸的是,xml 选项是有限的 - 它不适用于您的 xml 结构,因为它使用属性(
DataContractSerializer
仅支持元素)。您还可以查看此答案的评论,其中讨论了来自这。
The only way you could do that in a graceful way is to manually implement
IXmlSerializable
, which is not fun. Simply;XmlSerializer
doesn't support serialization callbacks.Sometimes, though, you can switch to
DataContractSerializer
, which still offers xml capabilities but which does support serialization callbacks. Unfortunately the xml options are limited - it won't work for you xml structure, since that uses attributes (DataContractSerializer
only supports elements).You might also look at the comments on this answer, which discusses the points from this.