August 21, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Inferring an XML Schema from an XML Document

  • September 3, 2008
  • 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"
           elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:element name="Root">
      <xs:complexType>
         <xs:sequence>
            <xs:element maxOccurs="unbounded" name="Customer">
               <xs:complexType>
                  <xs:sequence>
                     <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" />
                  </xs:sequence>
               </xs:complexType>
            </xs:element>
         </xs:sequence>
      </xs:complexType>
   </xs:element>
</xs:schema>

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.

Summary

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



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel