开发者

XSD Schema: Override an inherited element of a base complexType

We have started using schemas to generate C# classes for us and I am basically trying to replicate a class that has overridden a member inherited from it's base class. I have done a lot of searching around and am fairly new to XSDs so I don't know if I am going about it all wrong or if it's just not possible. Here is a simplified example of what I am trying to accomplish.

<xs:complexType name="ClientBase" abstract="true">
    <xs:sequence>
      <xs:element name="Company" type="CompanyBase" minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>  
  </xs:complexType>

  <xs:complexType name="CompanyBase" abstract="true">
    <xs:attribute name="Id" type="xs:int" use="required" />
  </xs:complexType>

  <xs:complexType name="Company">
    <xs:complexContent>
      <xs:extension base="CompanyBase">
        <xs:attribute name="PartnerId" type="xs:int" use="required" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="Client">
    <xs:complexContent>
      <xs:extension base="ClientBase">
        <xs:sequence>
          <!-- error occurs here -->
          <xs:element name="Company" type="Company" minOccurs="0" maxOccurs="unbounded" />
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

This obviously doesn't work as it blows up on my Client type giving me the error that "Multiple definition of element 'Company' causes the content model to become ambiguous".

My goal is to end up with something that represents this.

    public abstract class CompanyBa开发者_运维百科se
    {
        public int Id { get; set; }

    }
    public class Company : CompanyBase
    {
        public int PartnerId { get; set; }
    }

    public abstract class ClientBase
    {
        public CompanyBase[] Company { get; set; }
    }

    public class Client : ClientBase
    {
        new public Company[] Company { get; set; }
    }

Any help would be greatly appreciated.


Your complex type called Client, which you're specifying as having an element named Company, extends ClientBase, which already contains an element named Company (which is of type CompanyBase.) Hence, you have a redundancy in your element names.

Also: For your complex types, I recommend following a naming convention in your schemas that clarifies that they are types, not global elements. I.e., name your complex types as [Typename]Type, rather than [Typename]. It makes things much less confusing in the long run, and you can avoid name collisions with your global elements as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜