views:

203

answers:

8

I just learned about the XmlSerializer class in .Net. Before I had always parsed and written my XML using the standard classes. Before I dive into this, I am wondering if there are any cases where it is not the right option.

EDIT: By standard classes I mean XmlDocument, XmlElement, XmlAttribute...etc.

+3  A: 

I find the major drawbacks of the XmlSerializer are:

1) For complex object graphs involving collections, sometimes it is hard to get exactly the XML schema you want by using the serialization control attributes.

2) If you change the class definitions between one version of the app and the next, your files will become unreadable.

Mau
#2 is the big one I find a lot of programmers are afraid of
Neil N
@neil-n: Definitely!
Mau
It's still not as brittle as `BinarySerializer`
Callum Rogers
@callum-rogers: correct.
Mau
+2  A: 

Well, it doesn't give you quite as much control over the output, obviously. Personally I find LINQ to XML makes it sufficiently easy to write this by hand that I'm happy to do it that way, at least for reasonably small projects. If you're using .NET 3.5 or 4 but not using LINQ to XML, look into it straight away - it's much much nicer than the old DOM.

Sometimes it's nice to be able to take control over serialization and deserialization... especially when you change the layout of your data. If you're not in that situation and don't anticipate being in it, then the built-in XML serialization would probably be fine.

EDIT: I don't think XML serialization supports constructing genuinely immutable types, whereas this is obviously feasible from hand-built construction. As I'm a fan of immutability, that's definitely something I'd be concerned about. If you implement IXmlSerializable I believe you can make do with public immutability, but you still have to be privately mutable. Of course, I could be wrong - but it's worth checking.

Jon Skeet
A: 

Thera re some scenarios.

  • You have to deal with a LOT of XML data -the serializer may overlaod your memory. I had that once for a simple schema that contained a database dump for 2000 or so tables. Only a handfull of classes, but in the end serialization did not work - I had to use a SAX streaming parser.

Besides that - I do not see any under normal circumstances. It is a much easier way to deal with the XML Serializer than to use the lower level parser, especially for more complex data.

TomTom
+7  A: 

There are many constraints when you use the XmlSerializer:

  • You must have a public parameterless constructor
  • Only public properties are serialized
  • Interface types can't be serialized
  • and a few others...

These constraints often force you to make certain design decisions that are not the ones you would have made in other situations... and a tool that forces you to make bad design decisions is usually not a good thing ;)

That being said, it can be very handy when you need a quick way to store simple objects in XML format. I also like that fact that you have a pretty good control over the generated schema.

Thomas Levesque
+3  A: 

The XmlSerializer can save you a lot of trouble if you are regularly serializing and deserializing the same types, and if you need the serialized representations of those types to be consumable by different platforms (i.e. Java, Javascript, etc.) I do recommend using the XmlSerializer when you can, as it can alleviate a considerable amount of hassle trying to manage conversion from object graph to XML yourself.

There are some scenarios where use of XmlSerializer is not the best approach. Here are a few cases:

  • When you need to quickly, forward-only process large volumes of xml data
    • Use an XmlReader instead
  • When you need to perform repeated searches within an xml document using XPath
  • When the xml document structure is rather arbitrary, and does not regularly conform to a known object model
  • When the XmlSerializer imposes requirements that do not satisfy your design mandates:
    • Don't use it when you can't have a default public constructor
    • You can't use the xml serializer attributes to define xml variants of element and attribute names to conform to the necessary Xml schema
jrista
+1 for metnioning the size-argument and XmlReader as an alternative
Abel
A: 

When You want to transmit lot of data and You have very limited resources.

Vash
+1  A: 

Yes, I personally use automatic XML serialization - although I use DataContractSerializer initially brought in because of WCF instead (ability to serialize types without attributes at all is very helpful) as it doesn't embed types in there. Of course, you therefore need to know the type of object you are deserializing when loading back in.

The big problem with that is it's difficult to serialize to attributes as well without implementing IXmlSerializable on the type whose data you might want to be written so, or exposing some other types that the serializer can handle natively.

I guess the biggest gotcha with this is that you can't serialise interfaces automatically, because the DCS wants to be able to construct instances again when it receives the XML back. Standard collection interfaces, however, are supported natively.

All in all, though, I've found the DCS route to be the fastest and most pain-free way.

As an alternative, you could also investigate using Linq to XML to read and write the XML if you want total control - but you'll still have to process types on a member by member basis with this.

I've been looking at that recently (having avoided it like the plague because I couldn't see the point) after having read about it the early access of Jon Skeet's new book. Have to say - I'm most impressed with how easy it makes it to work with XML.

Andras Zoltan
+1  A: 

I've used XmlSerializer a lot in the past and will probably continue to use it. However, the greatest pitfall is one already mentioned above:

The constraints on the serializer (such as restriction to public members) either 1) impose design constraints on the class that have nothing to do with its primary function, or 2) force an increase in complexity in working around these constraints.

Of course, other methods of Xml serialization also increase the complexity.

So I guess my answer is that there's no right or wrong answer that fits all situations; chosing a serialization method is just one design consideration among many others.

TechNeilogy