提高XmlSerializer的性能 [英] Improve performance of XmlSerializer
问题描述
我用的XmlSerializer
序列化/反序列化的一些对象。问题是性能。在进行分析时,使用的XmlSerializer
让我们的应用2秒钟来启动。我们缓存我们的XmlSerializer和重用他们。因为我们正在创建与 XmlAttributeOverrides
XmlSerializer的,我们不能用sgen.exe。
我尝试使用序列化替代像Json.Net,并在第一,这是工作的伟大。问题是,我们需要让所有的XML已经生成需要被正确解析为向后兼容。 。此外,对象序列化输出必须是XML
要总结:
- 我收到一个XmlSerializer的序列化XML数据。
- 我要反序列化的XML数据,并将其转换成一个对象。
- 我需要序列化对象成XML(理想的XML格式像一个XmlSerializer的会做)
最后,这取决于模型的复杂性。 的XmlSerializer
需要做很多的思考,而事实上,它是这么长时间服用会导致我怀疑你的模型是非常复杂的。对于简单的模式,它可能是可以手动实现使用LINQ到XML(很容易),或反序列化甚至的XmlReader
(如果您感觉很勇敢 - 这是不容易得到正确率100%)
然而,如果模型是复杂的,这是一个问题,坦率地将在引入微妙的错误而言是非常危险的。
另一个选择是的DataContractSerializer
用于处理XML,但不作为以及的XmlSerializer
,肯定不会超过布局尽可能多的控制权。我强烈怀疑,的DataContractSerializer
也帮不了你。
有是没有直接替代XmlSerializer的
,我知道了,如果sgen.exe是不是一种选择,我相信你基本上选择:
- 忍受它
- 重写
的XmlSerializer
自己,不知何故做的比他们 - 更好使用类似LINQ到XML并接受参与
从长远来看,我会说转换格式,并利用工作XML的唯一遗产进口。我碰巧知道一些非常快的二进制协议,这将是很容易替代的; P
I use a XmlSerializer
to serialize/deserialize some objects. The problem is the performance. When profiling, using the XmlSerializer
make our application 2 seconds longer to start. We cache our XmlSerializer and reuse them. We cannot use sgen.exe because we are creating the XmlSerializer with XmlAttributeOverrides
.
I try to use serialization alternative like Json.Net and, at first, it's work great. The problem is that we need to be backward compatible so all the xml already generated need to be parsed correctly. Also, the object serialization output must be Xml.
To summarize:
- I receive Xml data serialized by a XmlSerializer.
- I need to deserialize the Xml data and convert it into an object.
- I need to serialize object into Xml (ideally an Xml format like the one a XmlSerializer would have done)
Ultimately, it depends on the complexity of your model. XmlSerializer
needs to do a lot of thinking, and the fact that it is taking so long leads me to suspect that your model is pretty complex. For a simple model, it might be possible to manually implement the deserialization using LINQ-to-XML (pretty easy), or maybe even XmlReader
(if you are feeling very brave - it isn't easy to get 100% correct).
However, if the model is complex, this is a problem and frankly would be very risky in terms of introducing subtle bugs.
Another option is DataContractSerializer
which handles xml, but not as well as XmlSerializer
, and certainly not with as much control over the layout. I strongly suspect that DataContractSerializer
would not help you.
There is no direct replacement for XmlSerializer
that I am aware of, and if sgen.exe isn't an option I believe you basically have options:
- live with it
- rewrite
XmlSerializer
yourself, somehow doing better than them - use something like LINQ-to-XML and accept the effort involved
Long term, I'd say "switch formats", and use xml for legacy import only. I happen to know of some very fast binary protocols that would be pretty easy to substitute in ;p
这篇关于提高XmlSerializer的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!