【问题标题】:XSD, restrictions and code generationXSD、限制和代码生成
【发布时间】:2011-01-22 06:35:26
【问题描述】:

我正在为现有项目生成一些代码,我想从 xsd 开始。所以我可以使用 Xsd2Code / xsd.exe 等工具来生成代码,也可以使用 xsd 来验证 xml。这部分工作没有任何问题。

我还想将一些限制转换为 DataAnnotations(丰富 Xsd2Code)。 例如 xs:minInclusive / xs:maxInclusive 我可以转换为 RangeAttribute。

但是如何处理我们创建的自定义验证属性呢?我可以添加自定义方面/限制吗?如何?或者是否有其他解决方案/最佳实践。

我想将所有内容收集到一个 (xsd) 文件中,以便一个文件包含类(模型)的结构,包括必须添加的验证(属性)。

<xs:element name="CertainValue">
  <xs:simpleType>
    <xs:restriction base="xs:double">
      <xs:minInclusive value="1" />
      <xs:maxInclusive value="100" />
      <xs_custom:customRule attribute="value" />
    </xs:restriction>
  </xs:simpleType>
</xs:element>

【问题讨论】:

    标签: c# xml code-generation xsd


    【解决方案1】:

    XML 模式本身是模式受限的,因此您不能添加任意(在其眼中)元素。有一个工具可以添加您喜欢的任何内容,xs:annotation/xs:appinfo,可用于添加到大多数节点。也许尝试这样的事情:

    <xs:element name="CertainValue">
    <xs:simpleType>
      <xs:restriction base="xs:double">
        <xs:annotation>
          <xs:appinfo>
            <xs_custom:customRule attribute="value" />       
          </xs:appinfo>
        </xs:annotation>
        <xs:minInclusive value="1" />
        <xs:maxInclusive value="100" />
      </xs:restriction>
    </xs:simpleType>
    

    这取决于 Xsd2Code 在哪里寻找这些东西,但如果他们希望传入的 XSD 进行验证,那么 appinfo 可能是他们唯一的选择。只是在哪个元素上添加 appinfo 也是个问题。

    【讨论】:

      猜你喜欢
      • 2010-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-28
      相关资源
      最近更新 更多