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
| XmlAnyAttribute | When deserializing, the array is filled with XmlAttribute objects that represent all XML attributes unknown to the schema. |
| XmlAnyElement | When deserializing, the array is filled with XmlElement objects that represent all XML elements unknown to the schema. |
| XmlArray | The members of the array are generated as members of an XML array. |
| XmlArrayItem | The derived types can be inserted into the array, used in conjuction with XmlArray. |
| XmlAttribute | Member is serialized as an XML Attribute |
| XmlChoiceIdentifier | Used in conjunction with enums |
| XmlElement | Serialized as an element. |
| XmlEnum | Element name of an enum member. |
| XmlIgnore | Exclude from serialization |
| XmlInclude | Should be included when generating schemas. |
| XmlRoot | Changes name of root namespace/elementname when serialized. |
| XmlText | Property serialized as XML text. |
| XmlType | The 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
}