Leveraging Our XML File for New Classes

Leveraging Our XML File for New Classes

Visual Studio .NET contains a handy XML schema definition tool that generates XML schema or common language runtime classes from XDR (Microsoft Extensible Data Reduced Schema Language), XML, and XSD files, or from classes in a runtime assembly. The tool, Xsd.exe, allows you to manipulate only XML schemas that follow the XML schema definition (XSD) language proposed by the W3C. (For more information on the XML schema definition proposal or the XML standard, see http://w3c.org.) We can first generate a stand-alone XML schema definition (XSD) from our SqlXML.xml file and then generate classes in Visual Basic .NET that we can use to build a program. Interested? Let's take a look.

The Xsd.exe Program

Locate the Xsd.exe file on your computer. On my machine it's located at C:\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Bin\Xsd.exe. Copy the file to the C:\ root directory where we wrote our XML files.

The first task we want to perform is to build the XSD file. Simply pass the name of the SqlXML.xml file as a command-line parameter to Xsd.exe.

C:\>xsd sqlxml.xml Microsoft (R) Xml Schemas/DataTypes support utility [Microsoft (R) .NET Framework, Version 1.0.3319.11] Copyright (C) Microsoft Corp. 1998-2001. All rights reserved. Writing file 'C:\sqlxml.xsd'

The Sqlxml.xsd file just created will be used to build our common language runtime Visual Basic .NET classes. Because the default language for class declarations is C#, we must tell Xsd.exe to spit out the file in Visual Basic .NET format. We pass in the name of our new .xsd file and use the command line directive /c to instruct Xsd.exe to build classes for us, and /l:vb to tell it to use the Visual Basic .NET language.

C:\>xsd sqlxml.xsd /c /l:vb Microsoft (R) Xml Schemas/DataTypes support utility [Microsoft (R) .NET Framework, Version 1.0.2914.11] Copyright (C) Microsoft Corp. 1998-2001. All rights reserved. Writing file 'C:\sqlxml.vb'

This file will be created in the directory where you build the program. The file created is called Sqlxml.vb and is constructed from our data set.

Visual Basic .NET generated a similar file, DataSet.vb, for the SqlXMLExample project. (Recall that when we built our inline schema, it had the default name DataSet1.) The code for the automatically generated new file spreads across acres of pages, so in the interest of brevity, I'll show only a small portion of it here. The important point to understand is that once you have a schema, all the information is present to automatically build Visual Basic .NET common language runtime classes that can be immediately imported into any Visual Basic .NET program.

'-------------------------------------------------------------- ' <autogenerated> ' This code was generated by a tool. ' Runtime Version: 1.0.2914.11 ' ' Changes to this file may cause incorrect behavior and ' will be lost if the code is regenerated. ' </autogenerated> '-------------------------------------------------------------- Option Strict Off Option Explicit On Imports System Imports System.Data Imports System.Runtime.Serialization Imports System.Xml <Serializable(), _ System.ComponentModel.DesignerCategoryAttribute("code")> _ Public Class DataSet1 Inherits System.Data.DataSet Private tableCategories As CategoriesDataTable Public Sub New() MyBase.New Me.InitClass End Sub Private Sub New(ByVal info As SerializationInfo, _ ByVal context As StreamingContext) MyBase.New Me.InitClass Me.GetSerializationData(info, context) End Sub <System.ComponentModel.Browsable(false), _ System.ComponentModel.DesignerSerializationVisibilityAttribute( _ System.ComponentModel.DesignerSerializationVisibility.Content)> Public ReadOnly Property Categories As CategoriesDataTable Get Return Me.tableCategories End Get End Property Protected Overrides Function ShouldSerializeTables() _ As Boolean Return false End Function Protected Overrides Function ShouldSerializeRelations() _ As Boolean Return false End Function Protected Overrides Sub ReadXmlSerializable(ByVal reader _ As XmlReader) Me.ReadXml(reader, XmlReadMode.IgnoreSchema) End Sub

If you scan the code, you'll see all of the properties required, such as those for getting and setting the Description property. Some sophisticated structured error-trapping code is also added for us. Following is the Description property code. Everything was written for us to both read and write the description of a category name. Pretty interesting, eh?

Public Property Description As String Get Try Return _ CType(Me( _ Me.tableCategories.DescriptionColumn), _ String) Catch e As InvalidCastException _ Throw New StrongTypingException( _ "Cannot get value because it is DBNull.", e) End Try End Get Set Me(Me.tableCategories.DescriptionColumn) = value End Set End Property

How might a class like this be used? I could send you an XML file without you having any previous knowledge of its contents. You would run it through the XSD program to build the XSD file, which you would then run through the Xsd.exe program again to build a robust Visual Basic .NET class that manages all of the editing for you. This new class could then be imported into a Visual Basic .NET program, and you could start handling all of the data I passed you in the XML file. (But we wouldn't learn much if we let all the code be written for us, would we?)

Examine this code some more to understand its finer points. For now, let's return to our program and see if we can enhance it a bit.



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