Thursday, March 29, 2007

XSD to .Net Class Code Generator

Code Project - An XSD to .NET language code class generator that adds real punch to Microsoft's XSD Tool

"In document-centric environments where data is persisted as XML documents, the need often arises to provide business logic at various stages during the processing of these documents. One way to do this is to use the System.Xml.XmlDocument class to 'front' the XML document and provide a means to modify the data. However, XmlDocument essentially models the XML structure of the document, and not the data structure of the document itself. Another way to do this would be to provide a data-access layer with classes that model the actual data structure and content itself, and provide serialization support to read and write the data to XML streams.

The advantages of the second approach are significant to the code developer and maintainer:

  • The classes can be re-used in the context of other data composition classes.
  • The developer works directly with classes and data types that are mostly domain-based, which is semantically easier to understand, more concise to code, and usually the code is a lot easier to read and maintain.
  • The code is usually easier to update as the data definitions change.
  • Visual Studio automatically provides intellisense prompting for these data types, so coding is faster and less error prone.

...

If the XSD.EXE generated classes are viewed as data-access classes, looking at the code, one realizes that the code is fairly complex to use and encapsulation is not really provided as all attributes are defined as public fields. In fact, the code, although simple, is quite ugly, and does not conform to many of the practices applied in the production of robust application code.

The purpose of this article is to demonstrate how the XSD tool output code can be modified to provide better encapsulation, robustness, and ease-of-use.

..."

I have a very near future feature where I need to consume a XML file, parse it and match/foreign key it to data in a DB. The producer of the XML file also provides an XSD...

Seeing the above project reminded me of xsd.exe and that this (and the above project) is something I will have to look very closely at (as I want to be as lazy as possible and don't want to write a bunch of parsing/etc code).

The one concern I have is that sometimes (so I'm told) the XSD may change (depending on project/client needs, etc). So that will need be taken into account (where as my cheese parsing could be made to handle that). Maybe the changes will not be dramatic enough to matter? Maybe I can consume the XSD at runtime and do some dynamic code generation. Maybe I'm being lame and xsd.exe doesn't actually do what I think it does (or need it to do)?

Maybe I just need to shut up and and give it a try.  ;)

5 comments:

Anonymous said...

I have some experience with this as my current project relies on modeling all of the data in an XML schema and storing the data in SQL 2005's XML data type.

XSD.exe has a few shortcomings which make it useful for all but the most simple schema.

Some of XSD.exe's shortcomings include its poor interpretation of xml types like xs:integer, its inability to generate anything but simple object arrays for list nodes (instead of ArrayLists or Typed Lists), and its really poor naming conventions.

A better tool is XSDObjectGen or even better, the latest XSDClassGen.

http://devauthority.com/blogs/ram_marappan/archive/2006/10/03/4755.aspx

Greg said...

You rock.. Thanks for the tip.

XSDClassgen looks very cool. I think I'll start there and just skip xsd.exe ;)

Thank you agin...

Anonymous said...

As for me, i'm seriously disappointed by the state of those tools. Ok, xsd.exe i did'nt try.

XsdObjectGen works in large parts, but with annoying bugs. I spent some days to produce workarounds. Unfortunately, it uses ArrayList for collections, which blocks a lot of the newer developments (Linq et. al)

XsdClassGen looks promising in this respect, but has serious bugs. For instance, it does not recognize attributes, a complete no-go. I cannot believe that anyone used it so far and did'nt notice this.

The latter 2 tools seem dead and abandoned by now (2008), no bugfixes or improvements to expect. The authors refrained from publishing the sources, so no one else can do anything about it. Sad...

(Did i miss something about class generation from xsd, is anybody using some other method and i am searching in the wrong direction?)

Anonymous said...

I have to agree with the last poster....you can't even download XsdClassGen anymore, becuase DevAuthority doesn't exist anymore. Even Ram's own blog page is kaput.

I can't believe that in this day and age that Microsoft oculdn't have added to XSD, the creation of
collections that don't use [] and use List[T] instead. That's a big, no HUGE, oversight.

Does ANYONE have a stray copy of XsdClassGen a guy could use in the meantime. Please forward it along to

marcelo_DOT_lopezjr_AT_gmail_DOT_COM

Greg said...

Here's a XSD to Code tool that might be interesting... (and supports List(T), etc

CSharp or Visual Basic class generator from XSD

"What Xsd2Code can do ?
Generate partial class.
Support generic collection (List(T), ObservableCollection(T)).
Support nillable type.
Mask private field in IDE (use EditorBrowsableState.Never attribute).
Generate object allocation in constructor.
Implement INotifyPropertyChanged for enable DataBinding.
Improves productivity with visual studio add-in. (new in release 2.1)
Generate summary documentation from xsd annotation. (new in release 2.1)
Check if the new and old values int setter are the same before raising property changed event. (new in release 2.1)
backup options generation in cs or vb header.
Generate CS or VB code.
..."

Hope this helps,
Greg