Click here to Skip to main content
Click here to Skip to main content

XML Schema Reader Writer Plugin for VS 2005/2008

, 17 Apr 2009
Rate this:
Please Sign up or sign in to vote.
Schema based XML reader writer implemented as .NET COM generator

Introduction

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.

The Problems

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.

The Idea

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...

The Solution

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 NRefactory libarary.

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.

pic2.JPG Click to enlarge image

pic3.JPG Click to enlarge image

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.
pic4.JPG Click to enlarge image

Usage sample:

pic5.JPG Click to enlarge image

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.

Requirements

  • Visual Studio 2005 or 2008
  • Windows SDK

History

  • 17th April, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

JadBenAutho
Software Developer Rontal Applications
Israel Israel
Born and raised in Israel, I've caught the programming virus at the age of 15.
Since than I can't stop coding.

Comments and Discussions

 
GeneralVS2010 PinmemberEvgueni Kolossov24-May-11 3:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140814.1 | Last Updated 17 Apr 2009
Article Copyright 2009 by JadBenAutho
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid