从C#中的类型生成程序化XSD - 在子类XSD中包含基类属性

时间:2022-10-02 17:18:35

I have a situation where we have a base class which several subclasses implement. The base class is used to force subclasses to contain certain properties that the system requires.

我有一种情况,我们有一个基类,几个子类实现。基类用于强制子类包含系统所需的某些属性。

Anyways, I'd like to programmatically generate XSDs for the subclasses, but I would like the properties of the base class to appear in the subclass XSD, because the base class is used for internal reasons and wouldn't really have meaning to the client.

无论如何,我想以编程方式为子类生成XSD,但我希望基类的属性出现在子类XSD中,因为基类是出于内部原因而使用,对客户端没有任何意义。

For example, currently if I have:

例如,目前如果我有:

class Foo {
    public string Id { get; set; }
}

class Bar : Foo {
    public string Name { get; set; }
}

And I run that through the following code:

我通过以下代码运行它:

private string ExtractXsdFromType()
{
    Type type = typeof(Bar);
    XmlReflectionImporter importer = new XmlReflectionImporter();
    XmlTypeMapping mapping = importer.ImportTypeMapping(type);
    XmlSchemas xmlSchemas = new XmlSchemas();
    XmlSchemaExporter xmlSchemaExporter = new XmlSchemaExporter(xmlSchemas);
    using (var writer = new StringWriter())
    {
        xmlSchemaExporter.ExportTypeMapping(mapping);
        xmlSchemas[0].Write(writer);
        return XElement.Parse(writer.ToString()).ToString();
    }
}

Then it will produce an XSD like this:

然后它将产生这样的XSD:

<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Bar" nillable="true" type="Bar" />
  <xs:complexType name="Bar">
    <xs:complexContent mixed="false">
      <xs:extension base="Foo">
        <xs:sequence>
          <xs:element minOccurs="0" maxOccurs="1" name="Name" type="xs:string" />
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="Foo" abstract="true">
    <xs:attribute name="Id" type="xs:int" use="required" />
  </xs:complexType>
</xs:schema>

But I would like to create an XSD like this:

但是我想像这样创建一个XSD:

<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Bar" nillable="true" type="Bar" />
  <xs:complexType name="Bar">
    <xs:complexContent mixed="false">
      <xs:sequence>
        <xs:attribute name="Id" type="xs:int" use="required" />
        <xs:element minOccurs="0" maxOccurs="1" name="Name" type="xs:string" />
      </xs:sequence>
    </xs:complexContent>
  </xs:complexType>
</xs:schema>

Does anyone know if this is possible?

有谁知道这是否可能?

1 个解决方案

#1


1  

I would be extremely surprised to actually see this possible the way you described simply because to do it like so would break things from an Object Orientation perspective, at least most of the time. For example, if a class uses a property of a type, then in OO any class extending that type would be allowed instead.

我会非常惊讶地看到这可能就像你描述的那样,因为这样做会使对象取向的观点破坏,至少在大多数时候。例如,如果类使用类型的属性,那么在OO中将允许任何扩展该类型的类。

Collapsing the class hierarchy (which is what you describe if I understood correctly), would make it impossible to correctly create an XSD model. There would be limitations even when trying to do so from an XSD refactoring perspective, particularly where abstract types get involved, or substitution groups (I am talking in general).

折叠类层次结构(如果我理解正确,这就是您所描述的),将无法正确创建XSD模型。即使从XSD重构角度尝试这样做也会有局限性,特别是涉及抽象类型或替换组(我正在谈论)。

#1


1  

I would be extremely surprised to actually see this possible the way you described simply because to do it like so would break things from an Object Orientation perspective, at least most of the time. For example, if a class uses a property of a type, then in OO any class extending that type would be allowed instead.

我会非常惊讶地看到这可能就像你描述的那样,因为这样做会使对象取向的观点破坏,至少在大多数时候。例如,如果类使用类型的属性,那么在OO中将允许任何扩展该类型的类。

Collapsing the class hierarchy (which is what you describe if I understood correctly), would make it impossible to correctly create an XSD model. There would be limitations even when trying to do so from an XSD refactoring perspective, particularly where abstract types get involved, or substitution groups (I am talking in general).

折叠类层次结构(如果我理解正确,这就是您所描述的),将无法正确创建XSD模型。即使从XSD重构角度尝试这样做也会有局限性,特别是涉及抽象类型或替换组(我正在谈论)。