1

我一直在寻找答案很长一段时间没有任何运气。

我正在为我的测试数据使用 NDbUnit,我希望能够自动生成我的 XSD 文件。我不想每次向我的类添加新属性时都重新生成我的 XSD 文件。所以我的计划是使用 XsdDataContractExporter 类自动生成 XSD 文件,因为我的一些类包含 IList 并且使用 xml 序列化程序时出现了一些错误。

这是我到目前为止得到的代码:

XsdDataContractExporter exporter = new XsdDataContractExporter();
exporter.Export(typeof(Supplier));

//XmlQualifiedName xmlQualifiedName = exporter.GetRootElementName(typeof(Supplier));
foreach (XmlSchema schema in exporter.Schemas.Schemas()) {
    XmlWriter writer = XmlWriter.Create(String.Format(@"..\..\data\{0}.xsd", GetSchemaName(schema)), new XmlWriterSettings() {
    ConformanceLevel = ConformanceLevel.Auto,
    Encoding = Encoding.UTF8,
    Indent = true,
    OmitXmlDeclaration = false
});

foreach (XmlSchemaObject include in schema.Includes) {
    XmlSchemaImport importedSchema = include as XmlSchemaImport;
    if (importedSchema != null) {
        ArrayList lst = new ArrayList(exporter.Schemas.Schemas(importedSchema.Namespace));
        XmlSchema actualImportedSchema = (XmlSchema)lst[0];
        importedSchema.Schema = actualImportedSchema;
        importedSchema.SchemaLocation = Path.GetFullPath(String.Format(@"..\..\data\{0}.xsd", GetSchemaName(actualImportedSchema)));
    }
}
schema.Write(writer);
writer.Close();
}
DataSet ds = new DataSet();
ds.ReadXmlSchema(@"..\..\data\business.model.supplier.xsd");

问题是我在读取架构时遇到错误:

failed: System.InvalidOperationException : Nested table 'SupplierCategory' which inherits its namespace cannot have multiple parent tables in different namespaces.

底线是,我想使用从一个类型动态生成的 XSD 文件与 NDBUnit 一起使用,用测试数据填充我的数据库。这是正确的方法吗?这是生成的模式,这给了我错误:

供应商:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:tns="http://schemas.datacontract.org/2004/07/Business.Model.Supplier" elementFormDefault="qualified" targetNamespace="http://schemas.datacontract.org/2004/07/Business.Model.Supplier" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:import schemaLocation="C:\dev\skeleton\branches\dev\src\testconsole\data\business.model.supplier.category.xsd" namespace="http://schemas.datacontract.org/2004/07/Business.Model.Supplier.Category" />
  <xs:import schemaLocation="C:\dev\skeleton\branches\dev\src\testconsole\data\arrays.xsd" namespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays" />
  <xs:complexType name="Supplier">
    <xs:sequence>
      <xs:element minOccurs="0" name="Categories" nillable="true" xmlns:q1="http://schemas.datacontract.org/2004/07/Business.Model.Supplier.Category" type="q1:ArrayOfSupplierCategory" />
      <xs:element minOccurs="0" name="Comments" nillable="true" xmlns:q2="http://schemas.microsoft.com/2003/10/Serialization/Arrays" type="q2:ArrayOfstring" />
      <xs:element minOccurs="0" name="Description" nillable="true" type="xs:string" />
      <xs:element minOccurs="0" name="Name" nillable="true" type="xs:string" />
    </xs:sequence>
  </xs:complexType>
  <xs:element name="Supplier" nillable="true" type="tns:Supplier" />
</xs:schema>

供应商类别:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:tns="http://schemas.datacontract.org/2004/07/Business.Model.Supplier.Category" elementFormDefault="qualified" targetNamespace="http://schemas.datacontract.org/2004/07/Business.Model.Supplier.Category" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="ArrayOfSupplierCategory">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="unbounded" name="SupplierCategory" nillable="true" type="tns:SupplierCategory" />
    </xs:sequence>
  </xs:complexType>
  <xs:element name="ArrayOfSupplierCategory" nillable="true" type="tns:ArrayOfSupplierCategory" />
  <xs:complexType name="SupplierCategory">
    <xs:sequence>
      <xs:element minOccurs="0" name="Description" nillable="true" type="xs:string" />
    </xs:sequence>
  </xs:complexType>
  <xs:element name="SupplierCategory" nillable="true" type="tns:SupplierCategory" />
</xs:schema>

任何建议都会很棒!谢谢,弗兰克

4

2 回答 2

0

请参阅这篇文章从 MS SQL 数据库获取 XML 模式(特别是投票最多的答案)中的建议列表,但也请注意我使用 MyGeneration 来实现相同目标的建议。

于 2011-03-18T13:48:26.787 回答
0

这是我很久以后也面临的一个问题,因为我们在项目中使用 NDbUnit 进行集成测试——最终得到了一个自定义库,它不需要单独定义架构,它从数据库中读取它在其自己的。库是Reseed

于 2021-11-11T12:17:58.300 回答