我不确定这是否是最好的方法,但我设法通过实施来做一些事情SchemaImporterExtension
:
namespace SchemaImport
{
public class ADODBSchemaImporterExtension : SchemaImporterExtension
{
public override CodeExpression ImportDefaultValue(string value, string type)
{
return new CodeTypeReferenceExpression(type);
}
public override string ImportSchemaType(XmlSchemaType type,
XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer,
CodeCompileUnit compileUnit, CodeNamespace codeNamespace,
CodeGenerationOptions options, CodeDomProvider codeGenerator)
{
return null;
}
public override string ImportSchemaType(string name,
string ns, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer,
CodeCompileUnit compileUnit, CodeNamespace mainNamespace,
CodeGenerationOptions options, CodeDomProvider codeProvider)
{
if (name.StartsWith("ADODB."))
{
compileUnit.ReferencedAssemblies.Add("adodb.dll");
mainNamespace.Imports.Add(new CodeNamespaceImport("ADODB"));
return name.Substring(name.IndexOf(".") + 1);
}
return null;
}
}
}
连同定义ADODB.Recordset
为xsd:complexType
:
<xs:element name="Table">
<xs:complexType>
<xs:all>
<!-- ...snip... -->
<xs:element name="CellValues" type="ADODB.RecordSet"/>
</xs:all>
</xs:complexType>
</xs:element>
<xs:complexType name="ADODB.Recordset"/>
然后我不得不将这个类添加到machine.config
:
<system.xml.serialization>
<schemaImporterExtensions>
<add type="SchemaImport.ADODBSchemaImporterExtension, SchemaImport, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cd583032ee337c41" />
</schemaImporterExtensions>
</system.xml.serialization>
/p:parameters.xml
并在xsd.exe的参数文件(开关)中指定程序集:
<?xml version="1.0" encoding="UTF-8"?>
<xsd xmlns='http://microsoft.com/dotnet/tools/xsd/'>
<generateClasses language='c#' namespace='TableDocument'>
<schemaImporterExtensions>
<type>SchemaImport.ADODBSchemaImporterExtension, SchemaImport, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cd583032ee337c41</type>
</schemaImporterExtensions>
</generateClasses>
</xsd>
using
最后,我在以 ADODB 作为引用和语句的 TableDocument 命名空间中得到了一个带有适当 Table 类的 .cs 文件。