XML and DataSets

Team Fly 

Page 493

image

FIGURE 17.1 The program atomizes an XML document into its smallest components.

XML and DataSets

Among the most useful aspects of XML in .NET is its interchangeability with DataSets. In this section you see how to create an XML schema that becomes a DataSet, then connect it to a DataGrid, and save or load this schema and the associated data.

Start a new VB.NET Windows-style project. Choose Project image Add New Item, then double-click the XML Schema icon in the dialog box. The Toolbox is now filled with the Tinkertoys you can use to build a schema. Double-click the Element icon in the Toolbox. A graphic appears in the design window, ready for you to define the structure (add attributes, for example). Each element in this kind of XML schema is the equivalent of a table in a database or DataSet.

Add several attributes to your element by dragging attribute icons from the Toolbox and dropping them into the element box graphic. You can adjust the data type in the right column of the element box by clicking, then dropping a list of available types. You can rename the attributes by clicking them, then typing.

Right-click the background of the design window and choose Generate DataSet from the context menu. Now click the Form1.vb [Design] tab to display your project's form. Click the Data tab on the Toolbox and double-click the DataSet icon. The Add DataSet dialog box opens with the name of your schema already displayed in the dialog box by default.

Click the Windows Forms tab on the Toolbox and add a DataGrid by double-clicking its icon. If you haven't experimented with it, the DataGrid is an excellent, flexible user-interface device for database work. In the Properties window, set the DataGrid's DataSource property to XmlSchema11.element1.

Now add some source code to permit you to persist and retrieve DataSets stored as XML files. Add two buttons to your form, then type Listing 17.7 in.

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