NT SP6 or higher and the free community edition of Visual Code-Generator (1.2.2 or higher)
VB, VB.NET, C# & Java
This article describes how to take an XML schema, and generate a set of wrapper classes from it that allow you to manipulate an XML document via simple strongly typed objects. Thus removing the need for developers to get bogged down in the complexities of schemas or deal with the nuts and bolts of MSXML. Using this technique you can produce wrapper classes for C#, VB.NET, VB6 & Java.
XML is now everywhere, its been universally embraced across the whole industry. It is fundamental to Web Services, .NET, J2EE & many, many more technologies. However its real potential is achieved when XML is used in conjunction with XML Schemas, without a schema there is nothing to formalise what is valid within your XML document. Without this, XML is reduced to another file format, to all intents an purposes as proprietary as a binary format.
So Schemas are a good thing?
Well of course, but they are not without issues. DTD has complex unintuitive syntax and is very limited. XDR is simple but a little too basic. XSD is extremely comprehensive, but very complex. That said, DTD & XDR are on there way out, and XSD has been accepted as the new standard, making it a one horse race.
This makes understanding XSD an essential skill required for working with XML. In order to actually work with XML itself you need to be very familiar with some kind of XML parser, MSXML is very good, comprehensive, but still quite complicated. All this to get to grips with before you've even started!
But what choice do you have?
Well lets look first at what we have.
XSD, a complicated machine readable description of what our XML document should look like.
MSXML, a complicated XML parser capable of reading and writing XML documents.
In essence you have everything you need to create a component that will take an XSD Schema, and generate a set of wrapper classes that abstract away all the details of XSD, XML, & MSXML. Leaving you to manipulate the data held in your XML documents.
So how does it work?
We have produced a template that can be used to generate a set of wrapper classes from an XDR or XSD schema. In this article we will look at the C# templates (there are others templates for different languages). First lets look at the schema file we want to wrapper.
Figure.1, shows a fragment from an XSD schema, it holds information about a
recipe has a number of
<xsd:element name="Summary" type="xsd:string"/>
<xsd:element name="Ingredient" type="Ingredient"
<xsd:element name="PreparationStep" type="PreparationStep"
<xsd:attribute name="Name" type="xsd:string" use="required"/>
<xsd:attribute name="PrepTime" type="xsd:unsignedShort"
<xsd:attribute name="CookingTime" type="xsd:unsignedShort"
If we wanted to wrapper this with a class, then we would probably want the
Recipe element to be represented as a class. This class would have a property
Name, and optional properties
We would also expect the elements
Ingredient to be represented as classes, and for our
recipe class to have a collection of
public String Summary
public IngredientCol IngredientsCol
public PreparationStepCol PreparationStepsCol
public String Name
public UInt16 PrepTime
public Boolean PrepTimeValid
public UInt16 CookingTime
public Boolean CookingTimeValid
Summary element is a little more tricky. You can see from the schema that it's an element, that represent a
string. Now we could take care of this by wrapping it with a class, but that seems a little over the top, so we'll make it a
property of the
Figure 2 shows a summary of the assessors for the generated
Recipe class. You may notice that because
CookingTime where optional within the schema, they have an addition property in the wrapper class
CookingTimeValid. These allow you to determine if these properties are valid within the object. If they are false then the indicate that the attribute is not in the XML document.
The templates produce the whole project, all the classes, the collections, all the methods, XML serialization etc. All you need to do is compile & use it! You can even place annotation blocks into the schema that allow you to document the generated code. This combined with .NET's documentation generator allows you to produce documentation for your new library.
Using the wrapper classes.
The wrapper classes couldn't be simpler to use. Figure 3 shows a how to use the example wrapper classes using VB.NET.
First we create an instance of the class we want to be the root element of our document, in this case
CookBookCls. We set the properties on this class in the same way we would any other object. If we want to add an
Ingredient object to the collection this can be done via the
IngredientCol collection, the
Add method returns the new
Ingredient object which we can populate.
PreparationStep collection is exactly the same.
Finally we want to see the XML that these objects represent. We can access this using the method
Dim oCookBook As New CookBook.CookBookCls()
Dim oRecipe As CookBook.RecipeCls
oCookBook.Name = "Adventures in food"
oCookBook.Author = "Mr Joe Blogs"
oRecipe = oCookBook.RecipeCol.Add
oRecipe.Name = "Blackberry Yummies"
oRecipe.Summary = "A perfect treat for summertime …"
oRecipe.CookingTime = Convert.ToUInt16(30)
oRecipe.PrepTime = Convert.ToUInt16(15)
.Amount = 3
.UnitEnum = CookBook.Unit.Unit_tbsp
.Description = "Butter"
.Amount = 1.25
.UnitEnum = CookBook.Unit.Unit_cup
.Description = "fresh blackberries"
.Summary = "Pre-Heat Oven"
.Details = "Preheat oven to 325 degrees C…"
.Summary = "Cook for 35-40 mins"
.Details = "Pour batter into the prepared…"
The following output is from the above sample code. As you can see the XML complies with the XSD schema that the wrapper classes were generated from
<CookBook Name="Adventures in food" Author="Mr Joe Blogs">
<Recipe Name="Blackberry Yummies" PrepTime="15" CookingTime="30">
<Summary>A perfect treat for summertime or ...</Summary>
<Ingredient Description="Butter" Amount="3" Unit="tbsp" />
<Ingredient Description="fresh blackberries" Amount="1.25" Unit="cup" />
<Details>Preheat oven to 325 degrees C ...</Details>
<Summary>Cook for 35-40 mins</Summary>
<Details>Pour batter into the prepared ...</Details>
But that's not the end of it all. Say you don't like the way our templates work, you want to add your own methods, or change the way we've implemented some of the functionality. Well with most generators this is a problem! You could request a change from the manufacturer, or change the generated code yourself. Neither option is very satisfactory. But with this generator you can change the templates.
Figure 5 shows a section from the template that generates the class files for C#. If you wanted to add a copy
constructor or change the names of the classes, this is simple to do. As mentioned before the templates work in exactly the same way as ASP & JSPs so the syntax should be very familiar.
Step By Step
- Open Visual Code-Generator
- Select menu File->Project Wizard
- Select a suitable location, and give the project a name
- Select the XML Schema Wrapper Workspace from the list and hit OK
- Select the kind of schema you want to use as a template (XSD or XDR)
- We will let the wizard give us a default schema to start with so just hit OK
- Accept all the prompts to create directories and files.
- You now have a new project.
- To build the wrapper from the Schema press F5.
- If you select the Output Tab you can see a list of all the files created.
- Job done, all you have to do now is open the project using .NET and built it.
- If you have your own schema, you can open the Project.xsd or Project.xdr file and cut and paste it into there, then just hit F5 again to re-build the output using your schema.
- If you want to edit the templates you can do by selecting the Templates tab, and double clicking on the template you want to change.
- Note you are working on a copy of the templates, changes made to this project will not effect other projects you may have created in the past.
Whatever your doing with XML, 9 times out of 10 you'll end up encapsulating the complexity of reading and writing from the XML document. These templates do that for you, saving 100's of man days, and with the flexibility to be changed in seconds. All you need is a valid XSD or XDR file, and in seconds you have a class library to work with instead of the customary messing around with MSXML.
XML Wrapper Template: Transform XML Documents into Visual Basic Classes
XML Code Generator: Generating Visual Basic Classes from XML Schemas