My Development Notes

By Haemoglobin
9/8/2010 (revision 2)

Serialization

BinaryFormatter bf = new BinaryFormatter(); / SoapFormatter(); 
bf.Serialize(fileStream, data); 
data = (ObjectType)bf.Deserialize(fs); 

[Serializable]
class MyClass : IDeserializationCallback {
    [NonSerialized] public int total; 
    public int price; 
    public int quantity; 
    //Field added in a later version
    [OptionalField] public bool taxable; 

    void IDeserializationCallback.OnDeserialization(object sender) {
        total = price * quantity; 
    }
}

[SoapAttribute]
[SoapDefaultValue]
[SoapElement]
[SoapEnum] //Element name of enum member
[SoapIgnore]
[SoapType] //The schema for the class used for serialization

XMLSerialization

  • Can only serialize public data
  • Can't serialize object graphs
  • Must have parameterless constructor
xs = new XmlSerializer(typeof(DateTime))
xs.Serialize(fileStream, DateTime.Now); //Can serialize DataGrid's too.
dt = (DateTime)xs.Deserialize(fs)

[XmlRoot("CertItem")]
public class ShoppingCartItem
    [XmlAttribute(AttributeName="category")] 
    public string ShoppingCategory;

   /* Use the XmlArrayItemAttribute to specify types allowed
      in an array of Object items. */
   [XmlArray]
   [XmlArrayItem (typeof(int), ElementName = "MyNumber"),
   XmlArrayItem (typeof(string), ElementName = "MyString"),
   XmlArrayItem(typeof(Manager))]
   public object [] ExtraInfo;
}

xsd c:\schema.xsd /classes /language:CS

DataSet.WriteXml, .ReadXml, .GetXml
XmlAnyAttributeWhen deserializing, the array is filled with XmlAttribute objects that represent all XML attributes unknown to the schema.
XmlAnyElementWhen deserializing, the array is filled with XmlElement objects that represent all XML elements unknown to the schema.
XmlArrayThe members of the array are generated as members of an XML array.
XmlArrayItemThe derived types can be inserted into the array, used in conjuction with XmlArray.
XmlAttributeMember is serialized as an XML Attribute
XmlChoiceIdentifierUsed in conjunction with enums
XmlElementSerialized as an element.
XmlEnumElement name of an enum member.
XmlIgnoreExclude from serialization
XmlIncludeShould be included when generating schemas.
XmlRootChanges name of root namespace/elementname when serialized.
XmlTextProperty serialized as XML text.
XmlTypeThe name and namespace of the XML type.

Serializing Dictionary types using DictionaryProxy http://johnwsaundersiii.spaces.live.com/blog/cns!600A2BE4A82EA0A6!699.entry

Custom Serialization

[Serializable]
class MyClass : ISerializable {
    //Constructor for deserialization
    protected MyClass(SerializationInfo info, StreamingContext context) {
        productID = info.GetInt32("Product ID");
    }

    [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
    public virtual void GetObjectData(SerializationInfo info, StreamingContext context) {
        info.AddValue("Product ID", productID); 
    }
}

Binary Formatter Serialization Events (for .NET to .NET)

    [OnSerializing]
    [OnSerialized]
    [OnDeserializing]
    [OnDeserialized]
    void CheckTotal(StreamingContext sc) {
        total = price * quantity;
        sc.Context  //(User context info) 
        sc.State    //CrossProcess, CrossMachine, File etc
    }

Comments

Powered by BlogEngine.NET 1.6.1.0 | Design by styleshout | Enhanced by GravityCube.net | 1.4.5 Changes by zembian.com | Adapted by HamishGraham.NET
(c) 2010 Hamish Graham. Banner Image (c) Chris Gin