简体   繁体   中英

Using xsd.exe to generate C# classes with SchemaImporterExtension. Without modifying machine.config

I have two different xsd files associated to two different VS2010 projects.

The first one, named BasicTypes.xsd is "built" first using xsd.exe and generated classes are built in my NetToolbox.dll assembly.

When building the second one (named Entity.xsd ), I need to reuse already generated class (Available in NetToolbox.dll ) because types in Entity.xsd are defined in BasicTypes.xsd . This is in assembly EntityProvider.dll .

Moreover, at runtime, I must pass these objects from EntityProvider.dll to NetToolbox.dll so having the same class definition is mandatory (I think, maybe I am wrong here).

This question is very similar to the following link but the proposed answer is not possible for me (Altering machine.config ) : Using xsd.exe to generate C# classes, how to specify an existing type?

I tried the proposed solution (By modifying machine.config ) without success I really had a hard time correctly specifying the assembly in both machine.config and xsd.exe parameter.xml file.

<system.xml.serialization>
    <schemaImporterExtensions> 
        <add type="SchemaImport.ADODBSchemaImporterExtension, SchemaImport, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cd583032ee337c41" /> 
    </schemaImporterExtensions>
</system.xml.serialization>

For example, here are the two XSDs. BasicTypes.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
             targetNamespace="http://www.tempuri.com/BasicTypes"
             xmlns="http://www.tempuri.com/BasicTypes"
             elementFormDefault="qualified">

    <xs:complexType name="PositionType">
        <xs:sequence>
            <xs:element name="latitude" type="xs:double" />
            <xs:element name="longitude" type="xs:double" />
            <xs:element name="altitude" type="xs:double" minOccurs="0" />
        </xs:sequence>
    </xs:complexType>
</xs:schema>

and Entity.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
             targetNamespace="http://www.tempuri.com/Entity"
             xmlns="http://www.tempuri.com/Entity"
             elementFormDefault="qualified"
             xmlns:bt="http://www.tempuri.com/BasicTypes">

    <xs:import namespace="http://www.tempuri.com/BasicTypes" schemaLocation="directory\BasicTypes.xsd" />

    <xs:element name="entity" type="EntityType" />

    <xs:complexType name="EntityType">
        <xs:complexContent>
            <xs:extension base="bt:PositionType">
                <xs:attribute name="id" type="xs:string" />
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:schema>

Using any other third party tool than the one provided by the .NET framework (I'm at 4.0) or VS2010 is not an option (Security restrictions)

Thank you

You can register the schema importer extension in a configuration file for the xsd.exe utility itself instead of in the machine.config file.

To do this, make a copy of the xsd.exe application somewhere in your project's directory structure and create a file named xsd.exe.Config in the same directory. Add the lines to register the schema importer extension inside a configuration root element.

For your example, the xsd.exe.Config file would look like the following. Note that a name attribute is required in the add element, I've used the name of the extension class.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.xml.serialization>
        <schemaImporterExtensions> 
            <add name="ADODBSchemaImporterExtension" type="SchemaImport.ADODBSchemaImporterExtension, SchemaImport, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cd583032ee337c41" /> 
        </schemaImporterExtensions>
    </system.xml.serialization>
</configuration>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM