For just such eventualities,
Whidbey defines SchemaImporterExtension; it sits in the middle of the XML Schema import process and maps types on the fly. Here is a sample that does the above mapping.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
using System;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.Xml.Serialization.Advanced;
using System.CodeDom;
using System.CodeDom.Compiler;
namespace Microsoft.Samples.Xml.Serialization.SchemaImporterExtension
{
publicclass ImportInteger
: System.Xml.Serialization.Advanced.SchemaImporterExtension
{
publicoverridestring ImportSchemaType(
string name,
string ns,
XmlSchemaObject context,
XmlSchemas schemas,
XmlSchemaImporter importer,
CodeCompileUnit compileUnit,
CodeNamespace mainNamespace,
CodeGenerationOptions options,
CodeDomProvider codeProvider
)
{
if (XmlSchema.Namespace == ns)
{
switch (name)
{
case"integer":
returntypeof(System.Int64).FullName;
case"negativeInteger":
returntypeof(System.Int64).FullName;
case"nonNegativeInteger":
returntypeof(System.UInt64).FullName;
case"nonPositiveInteger":
returntypeof(System.Int64).FullName;
case"positiveInteger":
returntypeof(System.UInt64).FullName;
default:
returnnull;
}
}
else
{
returnnull;
}
}
}
}
To use this, compile the above assembly, generate a strong name, add the assembly to the
GAC, and reference the assembly in
machine.config. Here is what
machine.config looks like on my machine:
<configuration>
<configSections>
...
</configSections>
...
<system.xml.serialization
> <schemaImporterExtensions>
<addname="MyImporter"type="Microsoft.Samples.Xml.Serialization.SchemaImporterExtension.ImportInteger, ImportInteger, Version=0.0.0.0, Culture=neutral, PublicKeyToken=3c3789dee90b3265" />
</schemaImporterExtensions>
</system.xml.serialization>
Because SchemaImporterExtension is integrated pretty deeply into the ASMX infrastructure, the good news is that the above just works with
Add Web Reference,
wsdl.exe, and
xsd.exe!
Posted
Jun 21 2005, 11:10 PM
by
jeffrey-schlimmer