I was going through articles to learn more about datacontractserializer and binary matrix serializers. Based on the readings made so far, I got the impression that the binary format should be smaller than the datacontractserializer. Reason: The DataContractSerializer is serialized to xml-infoset, while the binary format is being converted to its own binary format.
Below is the test
[Serializable]
[DataContract]
public class Packet
{
[DataMember]
public DataSet Data { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Description { get; set; }
}
DataSet was populated with 121317rows from [AdventureWorks].[Sales].[SalesOrderDetail]table
using (var fs = new FileStream("test1.txt", FileMode.Create))
{
var dcs = new DataContractSerializer(typeof(Packet));
dcs.WriteObject(fs, packet);
Console.WriteLine("Total bytes with dcs = " + fs.Length);
}
using(var fs = new FileStream("test2.txt", FileMode.Create))
{
var bf = new BinaryFormatter();
bf.Serialize(fs, packet);
Console.WriteLine("Total bytes with binaryformatter = " + fs.Length);
}
Results
Total bytes with dcs = 57133023
Total bytes with binaryformatter = 57133984
Question
Why is the number of bytes for binary formatting greater than the datacontractserializer? Isn't that a lot less?