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.
XmlSerializerneeds 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 evenXmlReader(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
DataContractSerializerwhich handles xml, but not as well asXmlSerializer, and certainly not with as much control over the layout. I strongly suspect thatDataContractSerializerwould not help you.There is no direct replacement for
XmlSerializerthat I am aware of, and if sgen.exe isn’t an option I believe you basically have options:XmlSerializeryourself, somehow doing better than themLong 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