Free mag vol1 | Page 930

CHAPTER 22  ADO.NET PART II: THE DISCONNECTED LAYER Figure 22-3. The XSD editor of Visual Studio  Note Chapter 24 will introduce you to the LINQ to XML API, which is the preferred manner for manipulating XML data in the .NET platform. Serializing DataTable/DataSet Objects in a Binary Format It is also possible to persist the contents of a DataSet (or an individual DataTable) as a compact binary format. This can be especially helpful when a DataSet object needs to be passed across a machine boundary (in the case of a distributed application). One drawback of XML data representation is that its descriptive nature can result in a good deal of overhead. To persist DataTables or DataSets in a binary format, set the RemotingFormat property to SerializationFormat.Binary. At this point, you can use the BinaryFormatter type (see Chapter 20) as expected. Consider the following final method of the SimpleDataSet project (don’t forget to import the System.IO and System.Runtime.Serialization.Formatters.Binary namespaces): static void SaveAndLoadAsBinary(DataSet carsInventoryDS) { // Set binary serialization flag. carsInventoryDS.RemotingFormat = SerializationFormat.Binary; // Save this DataSet as binary. FileStream fs = new FileStream("BinaryCars.bin", FileMode.Create); BinaryFormatter bFormat = new BinaryFormatter(); bFormat.Serialize(fs, carsInventoryDS); fs.Close(); // Clear out DataSet. carsInventoryDS.Clear(); 876