I followed the excellent MSDN article "Code Generation in the .NET Framework
Using XML Schema" [1] to build a substitute for the limited xsd.exe. My code
works fine under .NET framework 1.1, but after installing .net framework
service pack 1, my code breaks.
Below are trivial example files that demonstrate the problem:
XSD FILE aaa.xsd
----------------------
<?xml version="1.0" encoding="UTF-8"?>
<schema targetNamespace="urn:ietf:params:xml:ns:aaa"
xmlns:aaa="urn:ietf:params:xml:ns:aaa"
xmlns="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<complexType name="aaa">
</complexType>
</schema>
XSD FILE bbb.xsd
----------------------
<?xml version="1.0" encoding="UTF-8"?>
<schema targetNamespace="urn:ietf:params:xml:ns:bbb"
xmlns:bbb="urn:ietf:params:xml:ns:bbb"
xmlns:aaa="urn:ietf:params:xml:ns:aaa"
xmlns="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<import namespace="urn:ietf:params:xml:ns:aaa"
schemaLocation="aaa.xsd"/>
<element name="test"/>
</schema>
And here is the code which I use to process the above schema files (taken
almost line for line from the MSDN article mentioned above [1], but with the
addition of the ability to read multiple xsd files):
xsdCodeGen.cs
----------------------
string[] xsdFiles = { "aaa.xsd", "bbb.xsd" };
XmlSchemas schemas = new XmlSchemas();
// Load the XmlSchema and its collection
foreach(string filename in xsdFiles)
{
XmlSchema xsd;
using(FileStream fs = new FileStream(filename, FileMode.Open,
FileAccess.Read))
{
xsd = XmlSchema.Read(fs, null);
xsd.Compile(null);
}
schemas.Add(xsd);
}
// Create the importer for these schemas
XmlSchemaImporter importer = new XmlSchemaImporter(schemas);
// System.CodeDom namespace for the XmlCodeExporter to put classes in
CodeNamespace ns = new CodeNamespace("testNamespace");
XmlCodeExporter exporter = new XmlCodeExporter(ns);
// Iterate schema top-level elements and export code for each
foreach(XmlSchema xsd in schemas)
{
foreach (XmlSchemaElement element in xsd.Elements.Values)
{
// Import the mapping, and export the code
XmlTypeMapping mapping = importer.ImportTypeMapping(element.QualifiedName);
exporter.ExportTypeMapping(mapping);
}
}
The above code works fine pre- service pack, but post- service pack the line:
XmlTypeMapping mapping = importer.ImportTypeMapping(element.QualifiedName);
will throw the following exception:
Exception: The datatype 'urn:ietf:params:xml:ns:bbb:aaa' is missing.
Any ideas why the different behaviour pre- and post- service pack, and how
to fix?
Many thanks in advance,
Ceri Williams
[1]
http://msdn.microsoft.com/XML/Buildi...xsdcodegen.asp