Persisting Our XML Information

Persisting Our XML Information

Let's write our XML file of the data set to disk. We can persist the file and reconstitute it in this or another data set. Add another button to the program's form, and name it btnWriteXML. Add &Write XML as the Text property. Within the Click event handler of our new button, add the following two lines of code. This code permits us to write both the XML file with a built-in schema as well as a DiffGram, which notes only the differences between the original data and what we have changed.

Private Sub btnWriteXML_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnWriteXML.Click DataSet11.WriteXml("C:\SqlXML.xml", _ XmlWriteMode.WriteSchema) DataSet11.WriteXml("C:\SQLChanges.xml", _ XmlWriteMode.DiffGram)End Sub

When writing XML to disk, you have three options. The enumerated XmlWriteMode provides quite a bit of flexibility here, as shown in Table 11-3.

Table 11-3  XmlWriteMode Enumerated Values

XmlWriteMode

Description

IgnoreSchema

Writes the current contents of the DataSet as XML data, without an XSD schema. This is the default value.

WriteSchema

Writes the current contents of the DataSet as XML data with the relational structure as an inline XSD schema.

DiffGram

Writes the entire DataSet as a DiffGram, including original and current values.

Testing Our Persistence Code

To illustrate how the code we added to persist the XML file works, I modified the description for the dairy products category and then deleted record number 6, as you can see in Figure 11-9. (However, I didn't commit these changes via the Update Datasource button because it would modify the original Northwind table.) After I made these two changes, I clicked the Write XML button to serialize the output to XML files on disk.

Figure 11-9

The modified data set.

After clicking the Write XML button, examine the contents of the directory where the XML files were written. You will have two .xml files on your drive. In the first file, SqlXML.xml in this example, we wrote the XML file with the enumerated XmlWriteMode.WriteSchema. This instructs our program to write both the XML file and an inline schema. If you examine this file, you will note that the dairy products category has been modified. In addition, item number 6 is no longer there because we just deleted it. The header contains a new line with the version number. This line is called the declaration, and although it's optional, every written XML file should begin with an XML declaration. Here's the XML written to the file.

<?xml version="1.0" standalone="yes" ?> - <DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd"> - <xsd:schema targetNamespace="http://www.tempuri.org/DataSet1.xsd" xmlns="http://www.tempuri.org/DataSet1.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified"> - <xsd:element name="DataSet1" msdata:IsDataSet="true"> - <xsd:complexType> - <xsd:choice maxOccurs="unbounded"> - <xsd:element name="Categories"> - <xsd:complexType> - <xsd:sequence> <xsd:element name="CategoryID" msdata:ReadOnly="true" msdata:AutoIncrement="true" type="xsd:int" /> <xsd:element name="CategoryName" type="xsd:string" /> <xsd:element name="Description" type="xsd:string" minOccurs="0" /> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:choice> </xsd:complexType> - <xsd:unique name="Constraint1" msdata:PrimaryKey="true"> <xsd:selector xpath=".//Categories" /> <xsd:field xpath="CategoryID" /> </xsd:unique> </xsd:element> </xsd:schema> - <Categories> <CategoryID>1</CategoryID> <CategoryName>Beverages</CategoryName> <Description>Soft drinks, coffees, teas, beers, and ales</Description> </Categories> - <Categories> <CategoryID>2</CategoryID> <CategoryName>Condiments</CategoryName> <Description>Sweet and savory sauces, relishes, spreads, and seasonings</Description> </Categories> - <Categories> <CategoryID>3</CategoryID> <CategoryName>Confections</CategoryName> <Description>Desserts, candies, and sweet breads</Description> </Categories> - <Categories> <CategoryID>4</CategoryID> <CategoryName>Dairy Products</CategoryName> <Description>Cheeses and milk</Description> </Categories> - <Categories> <CategoryID>5</CategoryID> <CategoryName>Grains/Cereals</CategoryName> <Description>Breads, crackers, pasta, and cereal</Description> </Categories> - <Categories> <CategoryID>7</CategoryID> <CategoryName>Produce</CategoryName> <Description>Dried fruit and bean curd</Description> </Categories> - <Categories> <CategoryID>8</CategoryID> <CategoryName>Seafood</CategoryName> <Description>Seaweed and fish</Description> </Categories> </DataSet1>

Examining the DiffGram

A DiffGram is an XML serialization format that includes the original and current data of an element. It also includes a unique identifier that associates the original and current versions with one another. DiffGram objects are used for marshaling the data within a DataSet across a network connection so that different versions (original or current) and RowState values (added, modified, deleted, unchanged, detached) of the DataRow objects persist. Many times, you will add another data set to your program and insert a DiffGram from the original DataSet. You can examine and clean up an errors collection before committing the data to the source. When the errors are fixed, the DiffGram data set is merged with the original, the changes accepted, and the original data set updated.

If you examine the SQLChanges.xml file after making the changes I describe (the file is listed at the bottom of this page), you'll notice that the original records are listed first. The DiffGram gives each element a unique identifier—in this case Categories4 for the first change. It also adds the hasChanges attribute value of modified.

- <Categories diffgr: msdata:rowOrder="3" diffgr:hasChanges="modified"> <CategoryID>4</CategoryID> <CategoryName>Dairy Products</CategoryName> <Description>Cheeses and milk</Description> </Categories>

Later in the file, a hierarchical grouping starting with <diffgr: before> occurs. Between this tag and the delimiter, </diffgr: before>, are any changes to the original. This grouping contains both the modifications for dairy products and the deleted record. The DiffGram uses the id attribute to tie the original to the modified data. Because the ID Customers6 is not present in the original, it is implied that the record was deleted.

<diffgr:before> - <Categories diffgr: msdata:rowOrder="3" xmlns="http://www.tempuri.org/DataSet1.xsd"> <CategoryID>4</CategoryID> <CategoryName>Dairy Products</CategoryName> <Description>Cheeses</Description> </Categories> - <Categories diffgr: msdata:rowOrder="5" xmlns="http://www.tempuri.org/DataSet1.xsd"> <CategoryID>6</CategoryID> <CategoryName>Meat/Poultry</CategoryName> <Description>Prepared meats</Description> </Categories> </diffgr:before>

It's easy to see how the DiffGram flags differences between the original and modified data. And of course, it's all text.

<?xml version="1.0" standalone="yes" ?> - <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"> - <DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd"> - <Categories diffgr: msdata:rowOrder="0"> <CategoryID>1</CategoryID> <CategoryName>Beverages</CategoryName> <Description>Soft drinks, coffees, teas, beers, and ales</Description> </Categories> - <Categories diffgr: msdata:rowOrder="1"> <CategoryID>2</CategoryID> <CategoryName>Condiments</CategoryName> <Description>Sweet and savory sauces, relishes, spreads, and seasonings</Description> </Categories> - <Categories diffgr: msdata:rowOrder="2"> <CategoryID>3</CategoryID> <CategoryName>Confections</CategoryName> <Description>Desserts, candies, and sweet breads</Description> </Categories> - <Categories diffgr: msdata:rowOrder="3" diffgr:hasChanges="modified"> <CategoryID>4</CategoryID> <CategoryName>Dairy Products</CategoryName> <Description>Cheeses and milk</Description> </Categories> - <Categories diffgr: msdata:rowOrder="4"> <CategoryID>5</CategoryID> <CategoryName>Grains/Cereals</CategoryName> <Description>Breads, crackers, pasta, and cereal</Description> </Categories> - <Categories diffgr: msdata:rowOrder="6"> <CategoryID>7</CategoryID> <CategoryName>Produce</CategoryName> <Description>Dried fruit and bean curd</Description> </Categories> - <Categories diffgr: msdata:rowOrder="7"> <CategoryID>8</CategoryID> <CategoryName>Seafood</CategoryName> <Description>Seaweed and fish</Description> </Categories> </DataSet1> - <diffgr:before> - <Categories diffgr: msdata:rowOrder="3" xmlns="http://www.tempuri.org/DataSet1.xsd"> <CategoryID>4</CategoryID> <CategoryName>Dairy Products</CategoryName> <Description>Cheeses</Description> </Categories> - <Categories diffgr: msdata:rowOrder="5" xmlns="http://www.tempuri.org/DataSet1.xsd"> <CategoryID>6</CategoryID> <CategoryName>Meat/Poultry</CategoryName> <Description>Prepared meats</Description> </Categories> </diffgr:before> </diffgr:diffgram>



Coding Techniques for Microsoft Visual Basic. NET
Coding Techniques for Microsoft Visual Basic .NET
ISBN: 0735612544
EAN: 2147483647
Year: 2002
Pages: 123
Authors: John Connell

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