从架构生成Csharp文件(xsd)

时间:2015-03-17 09:39:25

标签: c# xsd

我使用xsd.exe生成此架构

<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:smp="http://iso.org/abc" elementFormDefault="qualified" targetNamespace="http://iso.org/abc" version="1.0.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="root">
    <xsd:complexType>
      <xsd:sequence>
          <xsd:element name ="InternalAddress" type="smp:InternalAddressType"></xsd:element>
          <xsd:element name ="Letter" type="smp:LetterType"></xsd:element>
       </xsd:sequence>
    </xsd:complexType>    
  </xsd:element>

  <xsd:complexType name="AddressType">
    <xsd:sequence>
      <xsd:element name="Line1" type="xsd:string"/>
      <xsd:element name="Line2" type="xsd:string" minOccurs="0" />
    </xsd:sequence>
  </xsd:complexType>

  <xsd:complexType name="InternalAddressType">
    <xsd:complexContent>
      <xsd:restriction base="smp:AddressType">
        <xsd:sequence>
          <xsd:element name="Line1" type="xsd:string" />
        </xsd:sequence>
      </xsd:restriction>
    </xsd:complexContent>
  </xsd:complexType>

  <xsd:simpleType name="LetterType">
    <xsd:restriction base="xsd:string">
      <xsd:pattern value="[a-zA-Z]"/>
    </xsd:restriction>
  </xsd:simpleType>

</xsd:schema>

结果:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://iso.org/abc")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://iso.org/abc", IsNullable=false)]
public partial class root {

    private InternalAddressType internalAddressField;

    private string letterField;

    /// <remarks/>
    public InternalAddressType InternalAddress {
        get {
            return this.internalAddressField;
        }
        set {
            this.internalAddressField = value;
        }
    }

    /// <remarks/>
    public string Letter {
        get {
            return this.letterField;
        }
        set {
            this.letterField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://iso.org/abc")]
public partial class InternalAddressType : AddressType {
}

/// <remarks/>
[System.Xml.Serialization.XmlIncludeAttribute(typeof(InternalAddressType))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://iso.org/abc")]
public partial class AddressType {

    private string line1Field;

    private string line2Field;

    /// <remarks/>
    public string Line1 {
        get {
            return this.line1Field;
        }
        set {
            this.line1Field = value;
        }
    }

    /// <remarks/>
    public string Line2 {
        get {
            return this.line2Field;
        }
        set {
            this.line2Field = value;
        }
    }
}

在Csharp代码中,InternalAddressType类型从AddressType派生所有元素而没有限制,尽管我禁止&#34; Line2&#34;。 有没有办法用模式中的限制生成Csharp代码?

1 个答案:

答案 0 :(得分:1)

不,您的XSD允许C#不允许的内容。继承要求您可以从基类访问每个public成员。

使用继承时,无法隐藏成员派生类。