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?
Those are the correct, current implementations for serialization.
The XMLSerializer is in the
System.Xml.Serializationnamespace instead of the System.Runtime namespace – I suspect this is because of its location (in the System.XML.dll assembly) and its dependencies on theSystem.Xmlnamespace 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: