Generate XSD Schemas by Inference
Microsoft's new XSD Inference Tool generates schemas from multiple XML source documents and solves most of Xsd.exe's problems.
by Roger Jennings
Posted November 11, 2002
The .NET Framework's Xsd.exe schema and class generation utility does a respectable job of generating XML Schema documents (XSDs) from instances of simple XML documents. Complex documents, however, often cause Xsd.exe to take a hike and leave behind mysterious error messages, such as "Can't find column 2." My "Xsd.exe Workarounds for Complex Documents" article describes five common Xsd.exe fatal error messages. Working around some errors, such as "The same table (row) cannot be the child table in two nested relations," requires changing the offending XML source document's tag names to conform to ADO.NET DataSet design rules and isn't likely to be practical in most cases.
Microsoft posted in late October 2002 the beta 1 version of a new .NET namespace, Microsoft.XsdInference, which contains an Infer class with a single, overloaded InferSchema method (see Resources). InferSchema generates an initial XSD schema from an XML source document and lets you refine the schema with additional related documents. I was surprised to find that InferSchema eliminated every Xsd.exe error thrown by the complex sample XML documents of my earlier article. Another benefit of InferSchema is elimination of Xsd.exe's gratuitous xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" namespace and msdata:IsDataSet="true" attribute of the root element.
The refining process adds or makes changes to the initial schema that accommodate differences in related source documents. If you have a class of documents that have structural variations, you can start with the simplest version and refine the schema to validate documents that include additional elements. This feature is especially useful when no single version of the source document contains all possible elements.
You can run the online XSD Inference 1.0 Beta Demo on the GotDotNet site with any XML source document that's smaller than 1M (see Figure 1). What the demo lacks is an independent XmlValidatingReader implementation to verify the resulting schema with multiple XML source documents. In this article, I'll show you how to take advantage of InferSchema's improved schema generation capabilities with a Visual Basic .NET test harness (XSDInferVB.sln) and sample XML documents that trip up Xsd.exe (see Figure 1). This Windows form project can validate the schema against all XML source documents of a single class. The project also displays the changes that occur when you refine the schema. An ASP.NET clone of the Windows form lets you preview the XSDInferVB project.
Install and Use the XSD Inference Tool
The test harness requires installation of two namespaces: Microsoft.XsdInference and Microsoft.XmlDiffPatch from the GotDotNet site. Click the Inference Demo page's Download link to save or run XSDInference.exe, which requires Windows Installer 2.0 (see Resources). Running XSDInference.exe creates a \Program Files\XSDInference folder with bin, Doc, Samples, and src subolders. The src subfolder contains almost 1,900 lines of hard-core C# source code for the bin subfolder's XSDInfer.dll. Documentation includes sample Visual Basic .NET and C# code for command-line examples that work with trivial XML documents.
Repeat the preceding process for XmlDiffPatch.exe, which installs a similar set of files in the \Program Files\XmlDiffPatch folder. Both installers add links to their help files to your Programs menu.
Back to top
|