January 20, 2021
Hot Topics:

Inferring an XML Schema from an XML Document

  • By Paul Kimmel
  • Send Email »
  • More Articles »

The code is pretty straightforward. Declare an XDocument and load the XML document customers.xml. Create the XmlSchemaInference class and a MemoryStream. Use the bytes from the XML document to initialize the MemoryStream. Use the MemoryStream to initialize an XmlTextReader.

The XmlTextReader will contain all of the schema sets detected in the XML document. There is only one in your document (from Listing 1). Create a StringWriter and write the schema to the StringWriter. A final XDocument and the Parse method accepts a TextWriter—the StringWriter's base class—and can construct an XDocument from the contents of the StringWriter.

Finally, call XDocument.ToString() to display the contents of the XDocument, which is an XML document containing the XSD for the XML in Listing 1. The inferred schema is shown in Listing 3.

Listing 3: The resulting XSD inferred from the XML document in Listing 1.

<xs:schema attributeFormDefault="unqualified"
   <xs:element name="Root">
            <xs:element maxOccurs="unbounded" name="Customer">
                     <xs:element name="CustomerID"
                                 type="xs:string" />
                     <xs:element name="CompanyName"
                                 type="xs:string" />
                     <xs:element name="ContactName"
                                 type="xs:string" />
                     <xs:element name="ContactTitle"
                                 type="xs:string" />
                     <xs:element name="Address" type="xs:string" />
                     <xs:element name="City" type="xs:string" />
                     <xs:element name="Region" />
                     <xs:element name="PostalCode"
                                 type="xs:unsignedShort" />
                     <xs:element name="Country" type="xs:string" />
                     <xs:element name="Phone" type="xs:string" />
                     <xs:element name="Fax" type="xs:string" />

That's it. Now you no longer have to write XSD from scratch. Start with an XML document that is an exemplar containing the data that you'd expect in all XML documents with a given schema and let .NET do the rest.

There are two things you can do from here. The first is you can tweak the XSD's type attributes to more closely match the desired data types, and the second is that you could create a macro or wizard in Visual Studio to integrate this solution into Visual Studio. That's left for the reader or another day.


If you are an XSD writing whiz, you probably write XSD all of the time. Even in that case, a tool that infers XSD from XML is going to be faster and less prone to syntax errors than writing XSD from scratch. With a few lines of code and a little know-how, you can let the .NET framework write your XSD for you.

About the Author

Paul Kimmel is the VB Today columnist for www.codeguru.com and has written several books on object-oriented programming and .NET. Check out his upcoming book LINQ Unleashed for C#; order your copy today at Amazon.com. Paul Kimmel is an Application Architect for EDS. You may contact him for technology questions at pkimmel@softconcepts.com.

Copyright © 2008 by Paul T. Kimmel. All Rights Reserved.

Page 2 of 2

This article was originally published on September 3, 2008

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date