Skip navigation

Generate XML Schemas With XSD

Get to know the .NET Framework’s serialization Swiss Army Knife.

ToolKit

LANGUAGES: C#

TECHNOLOGIES: XSD | ADO.NET | Serialization

 

Generate XML Schemas With XSD

Get to know the .NET Framework's serialization Swiss Army Knife.

 

By Ken McNamee

 

Tucked away in the .NET Framework's bin folder is a powerful tool that, although easy to overlook, can simplify your object-serialization efforts greatly. The XML Schema Definition (XSD) tool can take an assembly, reflect on the classes it contains, and output these classes' appropriate XML schemas. In addition, the tool can take a schema file, create the source code for a coordinating class in almost any .NET language, and optionally create the class as an object that inherits from DataSet.

 

If you're unfamiliar with XSD, think of it as a blueprint for your XML documents: It sets the boundaries for the types and structure of the data contained within them. XSD is somewhat analogous to how a class is a blueprint for an instance of an object. In fact, the reason the XSD tool exists lies in this relationship between data (as objects or XML) and metadata (as classes or XSD). Once you begin working with XSD and XML serialization in ASP.NET, you'll discover how closely they are woven into the entire class library, where many classes are converted easily into XML and back again. The DataSet class, however, is really the centerpiece of .NET's object/XML relationship. It has a host of orbiting classes you can use to create XML representations of your data that conform to a strongly typed schema.

 

Take this simple class as an example:

 

using System;

public class Customer

{

    int _customerID;

    string _name = string.Empty;

 

    public int CustomerID {

        get { return _customerID; }

        set { _customerID = value; }

    }

    public string Name {

        get { return _name; }

        set { _name = value; }

    }

}

 

To determine how the XML schema representation of the Customer class would look, simply compile the code into a Customer.dll file and execute this command-line statement:

 

xsd Customer.dll /t:Customer

 

This statement tells the XSD.exe tool to create the schema for the Customer class in the Customer.dll assembly. The /t switch allows you to choose which classes to convert to XSD. Otherwise, you can leave it out and XSD.exe operates against all the classes in that assembly.

 

The example XSD command produces this schema file:

 

<<?xml version="1.0" encoding="utf-8"?>>

<<xs:schema elementFormDefault="qualified" xmlns:xs="http://

               www.w3.org/2001/X...LSchema">>

  <<xs:element name="Customer" nillable="true"

   type="Customer" />>

  <<xs:complexTypc name="Customer">>

    <<xs:sequence>>

     <<xs:element minOccurs="1" maxOccurs="1"

      name="CustomerID" type="xs:int" />>

     <<xs:element minOccurs="0" maxOccurs="1"

      name="Name" type="xs:string" />>

    <</xs:sequence>>

  <</xs:complexTypc>>

<</xs:schema>>

 

If you use the XSD tool like this, you'll get an idea of how your classes will serialize and, more importantly, you'll get an opportunity to ensure the accuracy of the XML's final structure. You shouldn't wait until your B2B partners complain that the XML streams they're receiving from your site don't conform to the agreed specifications.

 

As you can see from the XSD output, the tool creates your class's fields and properties by default as child elements of the XML document's root element. If you need to create them as attributes of the Customer element instead, look no further than the System.Xml.Serialization namespace for the classes and examples you need to shape the XML. You can achieve the desired XSD output by including these attributes in the class definition:

 

using System;

using System.Xml.Serialization;

 

[XmlRootAttribute(ElementName = "Customer")]

public class Customer

{

    ...

 

     [XmlAttribute("CustomerID")]

    public int CustomerID { ... }

 

     [XmlAttribute("Name")]

    public string Name { ... }

}

 

But there is much more to the XSD tool than the class-to-schema process I've described. XSD.exe also can run in reverse and convert a schema file to the source code for a class with a conforming structure. One of the tool's most powerful features, however, is its ability not only to create a schema-conforming class, but also to create one that inherits from the DataSet class. This process generates about 400 lines of code for even the smallest schema, but it's well worth the effort to pore over the code to understand how this type of serialization works.

 

XSD and XML serialization are complex subjects, but you can reap enormous benefits with even a small amount of research. You might first begin thinking about your classes in terms of how they will serialize. This kind of from-the-ground-up XML planning is one of the core tenets of .NET, and it's quickly becoming the mantra of the 21st-century programmer. You're not a 20th-century programmer, are you?

 

References

 

 

Ken McNamee is a senior software engineer with RelayHealth Corp., a provider of secure, Web-based services for doctor-patient communication. Prior to this, he led a team of developers in rebuilding the Home Shopping Network's e-commerce site, HSN.com, to 100 percent ASP.NET with C#. E-mail Ken at mailto:[email protected].

 

Tell us what you think! Please send any comments about this article to [email protected]. Please include the article title and author.

 

 

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish