S h o r t S t o r i e s

// Tales from software development

Using the XSD Inference and XSD Object Code Generator tools

with one comment

I’ve been using these two tools to generate C# class code for serializable data for the past few years. It surprises me that some of my colleagues spend time hand coding data schemas. Maybe I’m just lazy…

XSD Inference tool

The XSD Inference tool was released back in 2002 and its functionality has now been integrated into the Visual Studio IDE (2005 and later). However, it’s still useful to have the tool because it allows you to incorporate the functionality into your build process.

There’s a help file provided that documents how dataypes are inferred.

XSD Object Code Generator

The XSD Object Code Generator tool was released in 2004 and is similar to the XSD tool that ships with Visual Studio.

The documentation is a Microsoft Word file.

Using together

I use the XSD Inference tool and the XSD Object Code Generator tool to create serializable C# classes from XML files containing examples of the XML data that these classes will be used to store. I guess I am being a bit lazy but it means that I don’t have to put any effort into the XSD schemas for persistent data. I just create a representative example of the data and run the two tools to generate the C# code.

When I first started using these tools, I’d typically create a folder called XsdInference in my project folder containing the example XML data files and a batch file that ran the two tools to generate the C# classes.

The real productivity gain comes when you need to change the data schema. A quick change to the example data, run the two tools, and your C# data class now supports the new schema.

The batch file contains these commands:

REM Configure these values as required:
SET ClassName=ConfigurationData
SET Namespace=MyProject.ConfigurationData

REM Run the inference and code generator tools:
"C:\Program Files\XSDInference\bin\Infer.exe" %ClassName%.template -o "%ClassName%.generated.xsd"
"C:\Program Files\XSDObjectGenerator\XSDObjectGen.exe" "%ClassName%.generated_1.xsd" /l:cs /n:%Namespace% /f:"..\%ClassName%.cs"

REM Remove the temporary schema file:
DEL "%ClassName%.generated_1.xsd"

Note that the schema file created by the XSD Inference tool always has a filename slightly different from that specified by the -o parameter. As can be seen in the batch file commands, when the -o parameter specifies filename.ext the tool actually generates the schema file as filename_1.ext.

Integrating with Visual Studio

I’ve made this slightly more sophisticated recently. It’s possible to run a batch file using the Pre-build event command line setting in the Build Events tab of the project properties dialog in Visual Studio. The simple solution is to execute the batch file that runs the XSD Inference tool and the XSD Object Code Generator tool.

However, this will mean that the inference and code generation tools run everytime you do a build in the IDE which is unnecessary.So, the first improvement is to run an MSBuild project file instead and use the Inputs and Outputs on a target to ensure that the tools are only executed when the example XML files have been changed.

There is another way to achieve this that provides tighter integration with the build process that Visual Studio uses. Open the .csproj file in a text editor and page down to the end of the file and you’ll see a commented out section with this text:

To modify your build process, add your task inside one of the targets below and uncomment it. 
Other similar extension points exist, see Microsoft.Common.targets.

This section can be used to call a target in your own MSBuild project. You’ll also need to code an Import statement for the project file. 

If you do this, not only will the target in your MSBuild project run each time the project is built in the Visual Studio IDE but it will have access to all the IDE’s build properties and items. As discussed in How to tell if your MSBuild project is running in Visual Studio, the BuildingInsideVisualStudio property may be used to determine if your build file is running as part of the IDE build process.


XSD Inference tool

XSD Object Code Generator tool


Written by Sea Monkey

December 5, 2008 at 9:00 pm

Posted in Development

Tagged with

One Response

Subscribe to comments with RSS.

  1. […] a follow-up to the posting Using the XSD Inference and XSD Object Code Generator Tools, how do you suppress analysis of the generated code […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: