0

I have an simple custom object called MyObject (a couple of basic properties and a List(of MyObject), so it's recursive) that I need to serialize for storage. I'm not sure if I'll serialize to XML or Binary yet, but I want to make sure I'm using the most up-to-date methods for doing it, as there are a few different namespaces involved and I might be missing something.

  • To do XML, I'd use System.Xml.Serialization.XmlSerializer
  • To do binary, I'd use System.Runtime.Serialization.Formatters.Binary.BinaryFormatter

There's also a System.Runtime.Serialization.Formatters.Soap.SoapFormatter, but MSDN claims it's depreciated in favor of BinaryFormatter. I would have expected everything to be in the second namespace above - is there a newer version of the XmlSerializer that I should be using?

SqlRyan
  • 33,116
  • 33
  • 114
  • 199
  • The question is subjective. Different serializers are for different purposes. Do you want deep serialization? Do you want to interop with non-CLR languages? – Justin Dearing Mar 09 '12 at 16:21
  • 1
    @Justin Dearning: My intent isn't really to ask for the "best" method in a subjective sense, but really to ask about the most current implementations for each type. I didn't want to continue using a deprecated or replaced implementation when a newer one was available in another namespace. Since I found what appeared to be the most current in different namespaces above (I'd expect them together), that's what prompted my question. Perhaps I can clarify the title. – SqlRyan Mar 13 '12 at 15:18

3 Answers3

2

Those are the correct, current implementations for serialization.

The XMLSerializer is in the System.Xml.Serialization namespace instead of the System.Runtime namespace - I suspect this is because of its location (in the System.XML.dll assembly) and its dependencies on the System.Xml namespace in general.

Also, FYI - when there are newer versions of a class that are to be used in favor of older versions, MSDN flags them as "Obsolete". For example, see XmlDataDocument's help - the first line is:

Note: This API is now obsolete.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • I realize I'm targeting .NET 2.0, even though the answer seems to be the same for 4.0. Nothing has changed in the last few years with regards to these methods? – SqlRyan Jun 22 '10 at 20:27
  • @rwmnau: Nope. The link I posted was .NET 4 - though you'll see in .NET 2 that link was the appropriate class. Luckily for you, in this case, you'll be using the same API that is current in .NET 4. – Reed Copsey Jun 22 '10 at 20:33
  • I mean that I'm targeting 2.0, and the namespaces I've referred to in my question appear to still be the best choice, since I assume that by "still current" you mean "as of 4.0, they're still the right choice". In any case, thanks for the affirmation - I was concerned that I was missing a much better choice somewhere in this gigantic framework... – SqlRyan Jun 22 '10 at 20:42
  • @rwmnau: Yes. The APIs listed are the best choice for those specific serialization forms in both .NET 2.0 and .NET 4.0 (and 3.5!). – Reed Copsey Jun 22 '10 at 20:43
2

There is also DataContractSerializer, which is as of .NET 3.5. It has some improvements over XmlSerializer in a several areas.

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx

code4life
  • 15,655
  • 7
  • 50
  • 82
1

For JSON serialization, you can use:

using System.Web.Script.Serialization;

...

JavaScriptSerializer().Serialize(PocoObject);

I had some difficulty getting this to work smoothly in .NET 2.0. See my answer to my own question here.

Community
  • 1
  • 1
Ben McCormack
  • 32,086
  • 48
  • 148
  • 223