Saving and Loading DataSet s as XML DiffGrams


Saving and Loading DataSet s as XML DiffGrams

The XML DiffGram is a format that makes it easy for two parties to refresh a shared data structure without having to transmit the entire data structure over the network. The desktop .NET Framework DataSet supports DiffGrams extensively. Thus, the desktop .NET Framework can use DiffGrams to support the following scenario:

  • Machine A creates a very large DataSet of 2000 records, one that requires nearly 1MB to transmit over the network as XML.

  • XML Data is flexible and human-readable . However, the cost is that XML data is not dense. Thus, even a modest amount of information can require a lot of text to be sent across the network. This is one of the reasons that the desktop .NET Framework DataSet extensively supports DiffGrams. It avoids having to retransmit redundant XML data.

  • Machine A uses DataSet.WriteXml() to export its dataset into a string and transmits the data to Machine B.

  • Machine B receives the XML and uses DataSet.ReadXml() to re-create a local copy of the DataSet .

  • Machine B adds tree records, removes one record, and updates values in one record in the DataSet .

  • Machine B sends a DiffGram back to Machine A. The DiffGram holds only five entries: three to describe the deleted records, one to describe the added record, and one to describe the updated record.

Machine B has used a DiffGram to describe updates to Machine A without having to transmit all of the redundant data back to Machine A.

The .NET Compact Framework's DataSet has very limited support for DiffGrams. It does not support the preceding scenario. Nonetheless, the scenario is very important to understand. If you try to write a .NET Compact Framework application that interacts with desktop applications, then you will run into trouble if the desktop applications try to use DiffGrams in the way described in the scenario.

The .NET Compact Framework DataSet supports DiffGrams under the following restrictions for reading and writing them:

  • The incoming DiffGram must include the contents of an entire DataSet . The entire DataSet must be valid. That is, it may not violate any constraints. It is possible for a desktop application to send a fraction of a DataSet in a DiffGram. The records in the DiffGram might violate the DataSet 's constraints if they are considered alone. When the .NET Compact Framework DataSet loads this DiffGram, it will consider the records as a stand-alone DataSet , discover that they are invalid, and throw an exception.

  • The .NET Compact Framework DataSet loading the DiffGram must be empty immediately before loading the DiffGram. This can be done by calling DataSet.Clear() or using a newly instantiated DataSet .

  • The incoming DiffGram may not have any data describing updated or deleted records.

  • When a .NET Compact Framework DataSet writes a DiffGram, it writes the contents of the entire DataSet .

  • There is no way to write only those records that have been added, updated, or deleted.

How to Read a Diffgram

The .NET Compact Framework DataSet can automatically infer that the XML data it reads is a DiffGram. If the file \ADSDiffgram.xml contains a valid DataSet in XML DiffGram format, then the following code will read it:

 
 C# System.Data.DataSet l_DataSet = new DataSet(); l_DataSet.ReadXml("\ADSDiffgram.xml"); // l_DataSet automatically realizes that the data is in DiffGram format VB Dim l_DataSet as System.Data.DataSet = new System.Data.DataSet l_DataSet.ReadXml("\ADSDiffgram.xml") ' l_DataSet automatically realizes that the data is in DiffGram format 

When to Use DiffGrams

As you have noticed, DiffGram support in the .NET Compact Framework DataSet is quite limited. It is present because the level of support is good enough for the .NET Compact Framework to send and receive DataSet s in Web service calls. Web services transmit DataSet s by using the DiffGram representation.

When a DataSet is passed as a Web service argument, the DiffGram representation is embedded in a SOAP packet for you automatically. If you write a very advanced application that builds and cracks SOAP packets without using the built-in Web service support described in Chapter 9, "Using XML Web Services," then you may be forced to deal with DiffGrams. However, implementing such an application is beyond the scope of this book.

There are no other major advantages to using the DiffGram format when programming with the .NET Compact Framework DataSet . Thus, we will not discuss the DiffGram format in great detail.



Microsoft.NET Compact Framework Kick Start
Microsoft .NET Compact Framework Kick Start
ISBN: 0672325705
EAN: 2147483647
Year: 2003
Pages: 206

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net