Team Fly |
Page 495
Then click the Save button and take a look at the file that VB.NET has saved:
<XMLSchema1 xmlns=''http://tempuri.org/XMLSchema1.xsd"> <element1 FirstName="Danny" attribute2= "Prior" attribute3= "12" attribute4= "23" /> <element1 FirstName="Hoda" attribute2="Macksoof" attribute3= "55" attribute4="-4" /> <element1 FirstName="Soledaa" attribute2="Nussy" attribute3="-2" attribute4="-5" /> </XMLSchema1>
This isn't your father's database. Stop the program, then run it again and click the Load button to populate the DataGrid.
Persisting arrays and collections (and arraylists, hashtables, what have you)—as well as objects, structures and so on—can be conveniently handled by an XML daughter technology, SOAP. You use the SoapFormatter to create or load an XML file that contains both the structure of the collection and its data. This technique draws upon the .NET serialization capability and is illustrated in Listing 17.8.
TIP XML serialization ignores any private fields (binary serialization saves both private and public fields).
WARNING To use XML serialization, you must choose Project Add Reference, then scroll down the list of "components" and add System.Runtime.Serialization.Formatters.Soap to your project. Oddly, some namespaces (mercifully only a few) must be added in this way—as a "reference"—to a project rather than employing the usual Imports statement. The distinction between which namespaces are imported and which assemblies must be added as "references" escapes me. It perhaps reveals which technologies were added to .NET late in the game. After you finish this step, you'll see the System. Runtime.Serialization.Formatters.Soap reference in the Solution Explorer, and you're ready to roll.
LISTING 17.8: PERSISTING TYPES VIA XML SERIALIZATION |
Imports System.IO |
Team Fly |