Persisting with SOAP

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 with SOAP

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 image 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
Imports System.Runtime.Serialization.Formatters
Imports System.Runtime.Serialization.Formatters.Binary

Private Sub Form1_Load(ByVal sender As System.Object, _
 ByVal e As System.EventArgs) Handles MyBase.Load

              Dim Arr(2), ArrNew(2) As String



              Arr(0) = "This test"
               Arr(1) = "continues until"
              Arr(2) = "it finishes." 

Team Fly 


Visual Basic  .NET Power Tools
Visual Basic .NET Power Tools
ISBN: 0782142427
EAN: 2147483647
Year: 2003
Pages: 178

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