This article describes a Visual Studio plug-in (for VS 2005/2008). that provides wider collaboration of XML schema (XSD) with the Visual Studio IDE and exposes the .NET XSD support for languages that aren’t supported by Microsoft CLR. In a nutshell, assembling this plug-in evolved using the xsd.exe utility that is part of Microsoft Windows SDK, .NET CodeDom and a little COM knowledge.
Several articles were written on CodeProject describing the XSD.exe tool and its various usages, for instance Serialization.aspx.
My plugin using the xsd.exe tool to create proxy classes that describe XSD file. The generated "Ordinary" proxy classes can be used for reading and writing objects to/from XML, and validating data according to a schema. All that without using too many lines of code.
For one, the generated code from the xsd.exe is far from being .NET optimized, nor is it integrated in Visual Studio. In order to generate the XML proxy code, one should step out of VS, execute the xsd.exe with predefined XML schema, renter VS, add the proxy code to the project. (That can only be a .NET language one, which leaves C++ out.)
Reading and validating data requires creating XML serializer object,
XmlReader, configuring the
XmlReader to the schema type, loading the schema (same schema we used to create the objects in the first place!!!).
All in all, doing it over and over again, especially when the schema changes rapidly, sounds exhausting. True, from time to time Visual Studio creates the proxy classes by itself when dragging a schema file to .NET project, but what about schema changes?
Bottom line - Visual Studio itself does not work tightly with xsd.exe.
I've discovered the power and potential of the xsd.exe long time ago but always had the excuse of Visual Studio compatibility, the "hard" work that I've described above and the .NET exclusivity over it was also reason to avoid it. But what if the proxy classes were COM exposed? What if they had COM interfaces. How cool would it be to harness .NET power to languages such as native C++? What if the writing, reading and validating processes were part of the proxy code?
A plugin to Visual Studio that will do all that could be nice...
I started with code that will auto generate parallel COM class/enumeration to each xsd.exe proxy type (I call it raw type) and corresponding COM interface to each class that required the usage of the
System.CodeDom namespace for dynamic code generation and parsing on the raw code from xsd.exe, that was done using SharpDevelop's
Each new class I'm building can be converted to the raw proxy type for writing. The reversed process takes place when reading data. Once the dynamic coding is done, the code is compiled and ready to be used with a lot greater simplicity.
Using the Plugin
After the plugin is installed (setup project is included), start Visual Studio and load project that contains one or more valid XSD (XML schema) file.
Once the wizard execution is over, folder will be added to the project with the output of the wizard:
- A .NET class library file contains the reading/writing code.
- An XML documentation file of the class library.
- A readme file contains the .NET library file GUID (
LIBID), the GUIDs of
Factory class and interface uses to create all the needed components.
In order to validate the data you're reading, use another overload of
CreateReader method of the factory and supply your own validator class instance.
- Visual Studio 2005 or 2008
- Windows SDK
- 17th April, 2009: Initial post