I have a DataSet with some DataTables. I can binary serialize a DataSet successfully.
If I try to serialize one of the DataTables separately, I get a NullReferenceException.
If I make a copy of the DataTable, then copying will be fine.
Does anyone know what I'm doing wrong?
This code reproduces the problem:
internal class Program { private static void Main(string[] args) { System.String xml = "<CR><AN>543</AN><Br>XYZ</Br><Id>888</Id><M>123456</M><EVT>DATA</EVT><data><telephony><S>2012-01-11T14:01:54.9571247Z</S><CID>100000</CID><reason>test</reason></telephony></data></CR>"; var ds = new System.Data.DataSet(); using (var stringReader = new System.IO.StringReader(xml)) { ds.ReadXml(stringReader); } //This works. var serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); using (var memStream = new System.IO.MemoryStream()) { serializer.Serialize(memStream, ds); } System.Data.DataTable dt0 = ds.Tables[0]; //Serializing a copy works System.Data.DataTable copy = dt0.Copy(); using (var memStream = new System.IO.MemoryStream()) { serializer.Serialize(memStream, copy); } //Serializing the original fails with a NULL Reference Exception using (var memStream = new System.IO.MemoryStream()) { serializer.Serialize(memStream, dt0); } } }
source share