Reading XML into a Visual Basic .NET Dataset

 <  Day Day Up  >  

Reading XML into a dataset in Visual Basic .NET and displaying it in a grid is even easier because XML is the native data format. The following three lines of code in the Load event of a form containing a grid will do the same thing that we did in the preceding FoxPro example:

 

 Dim DS As New DataSet DS.ReadXml("C:\XML1.XML") DataGrid1.DataSource = DS.Tables(0) 

That's probably the first time we've seen something that's easier in Visual Basic .NET than it is in FoxPro. But there are a few more as well, as we'll see in later chapters.

TIP

These three lines ofVisual Basic .NET code are pretty cryptic, so let me explain what they do: The first line declares a variable of type DataSet and creates one in a single line; some purists think that's inelegant, but I don't. The second line uses the DataSet 's native ReadXML method (or rather, one of about eight overloads [versions] of the ReadXML method that the DataSet has, which can read XML directly from a file). Finally, a grid's data source is not a DataSet , but rather one of the Tables in a DataSet . Tables(0) , according to Bill's weird arithmetic, is the first one. Voil  .


The Visual FoxPro XMLAdapter

The XMLAdapter class is also new in Visual FoxPro 8. It's similar to the CursorAdapter class, except that it has more methods specifically designed for dealing with XML. It contains most, if not all, of the functions we need in Visual FoxPro to manipulate XML, and this makes the job much easier than it was in Visual FoxPro 7.

In order to use it, you first have to load an XML Document. You can do so using its LoadXML() method to load XML from either a string variable or from a file. Or, you can use Attach() to load an XML document that's already been loaded into an XMLDOMDocument object, or from HTTP. You can also use AddTableSchema to read a FoxPro cursor.

The result is the same: An XML document is loaded into the XMLAdapter object, at which point you can call other XMLAdapter methods. This is a two-way class: Some methods load XML and another writes XML. There are also other utility methods. Table 7.3 contains a preview.

Table 7.3. XMLAdapter alternate utility methods

Method

Description

AddTableSchema

Load a schema for a table, as well as its data

Attach

Load an XMLDOM object previously loaded by another object, for example by an XMLHTTP object (see example below)

LoadXML (String/filename)

Loads XML from a string or table

ToXML

Creates a variable of type String or a file and fills it with the XML representation of the currently loaded document


 <  Day Day Up  >  


Visual Fox Pro to Visual Basic.NET
Visual FoxPro to Visual Basic .NET
ISBN: 0672326493
EAN: 2147483647
Year: 2004
Pages: 130
Authors: Les Pinter

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