Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any reason not to use XmlSerializer?

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.

like image 588
JimDaniel Avatar asked Jul 13 '10 16:07

JimDaniel


People also ask

Why do we use XmlSerializer class?

Serialization/ De-serialization allow communication with another application by sending and receiving data. With XmlSerializer, you can control how objects are encoded into XML. Call the Serialize method with the parameters of the StreamWriter and object to serialize.

Can I make XmlSerializer ignore the namespace on Deserialization?

Yes, you can tell the XmlSerializer to ignore namespaces during de-serialization. Note this is the kind of thing I meant. You are not telling the XmlSerializer to ignore namespaces - you are giving it XML that has no namespaces.

How does the XmlSerializer work C#?

The XmlSerializer creates C# (. cs) files and compiles them into . dll files in the directory named by the TEMP environment variable; serialization occurs with those DLLs. These serialization assemblies can be generated in advance and signed by using the SGen.exe tool.

Which namespace is used in WCF for data serialization?

DataContractSerializer as the Default By default WCF uses the DataContractSerializer class to serialize data types.


2 Answers

There are many constraints when you use the XmlSerializer:

  • You must have a public parameterless constructor (as mentioned by idlewire in the comments, it doesn't have to be public)
  • 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.

like image 111
Thomas Levesque Avatar answered Sep 19 '22 13:09

Thomas Levesque


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.

like image 40
Jon Skeet Avatar answered Sep 18 '22 13:09

Jon Skeet