|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionIn the previous article, I discussed about how .NET does serialization and converts the objects to XML and vice versa in a Web service scenario. In this article, I will discuss more about .NET serialization classes and how to use them. Custom Serialization & DeserializationCustom serialization is the process in which we determine how the object should be converted into a stream of bytes. .NET offers different ways of custom serialization. Using AttributesAttributes are applied at the class level and mark that the class instance can be serialized. The properties,
If you don't need a field to be serialized, you can mark it as [Serializable]
public class Customer
{
[XmlAttribute("ID")]
public string Id;
[XmlElement("Name")]
public string Name;
[XmlElement("Hire")]
public DateTime HireDate;
[XmlElement("Rate")]
public decimal RatePerHour;
[XmlAttribute("Dob")]
public DateTime Dob;
public Customer()
{
}
}
Once you define the entity class, you can serialize and deserialize the instances using XmlSerializer serializer = new XmlSerializer(typeof(Customer));
StringWriter writer = new StringWriter();
serializer.Serialize(writer, customer);
textBoxResult.Text = writer.ToString();
Deserialization is the reverse process, and the code snippet is given below: StringReader reader = new StringReader(textBoxResult.Text);
XmlSerializer serializer = new XmlSerializer(typeof(Customer));
Customer customer = (Customer) serializer.Deserialize(reader);
Pros and ConsThis approach has the following advantages:
This approach has the following pitfalls:
Using IXmlSerializable InterfaceThe second technique of custom serialization & deserialization is by implementing the The entity class doesn't need any attribute at the class level or at the property/field level. The runtime would automatically consider all the In the public void WriteXml(XmlWriter writer)
{
writer.WriteAttributeString("Id", Id);
writer.WriteAttributeString("Name", Name);
writer.WriteAttributeString("Quantity", Quantity.ToString());
writer.WriteAttributeString("Rate", Rate.ToString());
}
In the public void ReadXml(XmlReader reader)
{
Id = reader.GetAttribute("Id");
Name = reader.GetAttribute("Name");
Quantity = int.Parse(reader.GetAttribute("Quantity"));
Rate = decimal.Parse(reader.GetAttribute("Rate")) ;
}
Pros and ConsThis approach has the following advantages:
This approach has the following pitfalls:
Using ISerializable InterfaceCustom serialization can also be achieved by implementing This works very similar to the Serializing using public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Id", _id);
info.AddValue("OrderDate", _orderDate);
info.AddValue("Customer", customer);
info.AddValue("Products", products);
}
Deserializing using public Order(SerializationInfo info, StreamingContext context)
{
_id = info.GetString("Id");
_orderDate = info.GetDateTime("OrderDate");
customer = (Customer)info.GetValue("Customer", typeof(Customer));
products = (Product[])info.GetValue("Products", typeof(Product[]));
}
The The code that invokes the serialization process is as follows: MemoryStream memoryStream = new MemoryStream();
StreamWriter writer = new StreamWriter(memoryStream);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(memoryStream, order);
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] bytes = memoryStream.ToArray();
The code that invokes the deserialization process is as follows: MemoryStream memoryStream = new MemoryStream(bytes);
BinaryFormatter formatter = new BinaryFormatter();
Order order = (Order) formatter.Deserialize(memoryStream);
Pros and ConsThis approach has the following advantages:
This approach has the following pitfalls:
About the CodeThe application has three forms:
Each form illustrates a different technique. The form would serialize the instance and show the serialized data in the text box in which the deserialize function would form the instance and fill the controls. You can change the value of the serialized data and see the deserialization process pick the changed data and fill the controls. The The The
The Conclusion.NET Framework provides many options for performing custom serialization and deserialization of class instances. Based on various factors like flexibility, interoperability, code maintainability, effort involved, we can choose the appropriate technique. In the next article, I shall discuss how we can completely implement a serialization engine from scratch, with interoperability and flexibility as key requirements. Happy reading! Happy coding! History
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||