Generate XSD from .NET Class

As BizTalk developers we often use XSD.EXE to generate C# classes from XSD.  This is typically because we start with XML Schema when developing integration scenarios.  Today was the first time that I actually went the other way and had to generate an XSD from a pre-built class.  See below example,

Define a class marking declarations with metadata attributes.  Note the use of XmlAttribute and XmlArrayItemAttribute.  This metadata is used by the serialization classes to properly format XML as its serialized and de-serialized. 


Now run XSD.EXE from the Visual Studio command prompt. This command line is telling the tool to generate a schema specifically for the BusinessDocument type within the BusinessDocument.dll assembly.

The resulting XSD can be seen below.  Note how the root node name is the class name, the declarations that were marked as XmlAttribute within the BusinessDocument class are XSD attributes, and the item field declaration marked as XmlArrayItemAttribute within the BusinessDocument class is modelled as a repeating group.

An XML document generated in accordance with this schema can be de-serialized directly to an object of type BusinessDocument and a BusinessDocument object can be seralized directly to XML that conforms to the generated XSD.

Technorati Tags:

This entry was posted in .NET. Bookmark the permalink.

Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s