- Use XML serialization when you need to exchange an object with an application that
might not be based on the .NET Framework - Private members of a class are not serialized in XML. This means only public data is serialized.
- System.Xml.Serialization namespace is required.
- Xml serialization does not need [Serializable] attribute on a class. However the class must be public and should have a parameter-less constructor.
- XmlIgnore Attribute is similar to NonSerialized Attribute.
- Xml Schema Definition tool (Xsd.exe) is used to generate set of classes that are strongly typed according to a schema.
- DataSet.WriteXml( )/ReadXml( ) methods may be used to serialize datasets into XML.
- For other types of collections, use TextWriter, initialize it as a StreamWriter and supply this object to XmlSerializer instead of filename in first argument. Collection could be in second argument of Serialize( ) method.
Showing posts with label Serialization. Show all posts
Showing posts with label Serialization. Show all posts
Thursday, June 25, 2009
Chapter 5-Lesson 2
Monday, June 22, 2009
Chapter 5-Lesson 1
- Converting Object information into another format is called Serialization.
- System.Runtime.Serialization namespace is required
- Items copied to clipboard, Remoting and Web Services are good examples of Serialization
- For Binary Serialization, we use System.Runtime.Serialization.Formatters.Binary namespace BinaryFormatter class.
- The terms 'Backward Reference' and 'Forward Reference' are used during Deserialization process when an object being Deserialized refers to another object.
- If the referenced object is already Deserialized, the Formatter treats it a Backward Reference and completes the reference immediatly.
- However, if the referenced object is not Deserialized yet, it is treated as a Forward Reference and the Formatter registers a fix-up with the ObjectManager which would later on complete the reference.
- Serializable attribute allows a custom class object to be serialized by a Formatter. All members (including private ones) are serialized.
- NonSerialized attribute before any class member would omit it from (De)Serialization process.
- IDeserializationCallback.OnDeserialization( ) method may then be used to initialize objects which were omitted during (De)Serialization.
- In situations where after serialization, one adds additional members to a class, deserialization may fail because serialized object would not contain info about that additional field. Therefore, if a class has previously been used for Serialization, its always good to use OptionalField attribute to newly added members for sake of Version Compatibility.
- If members were removed from a class after some objects were serialized, .NET 2.0 would NOT throw an exception while deserializing (since there would be more info available than requested for deserialization). Older versions on .NET did.
- If object is serialized so that it would later be read by ONLY .NET based applications, use of BinaryFormatter is preferrable. Otherwise use SOAPFormatter.
- Using SOAPFormatter requires the project to have a reference to System.Runtime.Serialization.Formatters.Soap.dll which is not included by default in .NET 2.0
- Use BinaryFormatter for best efficiency and SOAPFormatter for portability.
- SoapIgnore attribute is used for public members of class which we do not wish to serialized to XML.
Subscribe to:
Posts (Atom)