c# - Xml 验证错误根据其数据类型 'CopyFrom',该值无效 - 枚举约束失败

标签 c# xml validation

我正在尝试验证 Xml 文件

我的 xsd 架构片段:

<xs:attribute name="PostIndex" use="optional">
                        <xs:annotation>
                            <xs:documentation>Post Index</xs:documentation>
                        </xs:annotation>
                        <xs:simpleType>
                            <xs:restriction base="xs:string">
                                <xs:length value="6"/>
                                <xs:pattern value="\d{0}|\d{6}"/>
                            </xs:restriction>
                        </xs:simpleType>
                    </xs:attribute>

XML 文件片段:

<Atr1>
<Atr2 Atr="A9F130BE-3974-4698-B9F9-72037BC0E97F" PostIndex="123456" />
<Atr2 Atr3="123" Atr4="11111" />
</Atr1>

当我运行验证代码时,它通过了模式验证我有错误:

The 'PostIndex' attribute is invalid - The value '123456' is invalid according to its datatype 'String' - The Enumeration constraint failed.

最佳答案

这是我使用的 XSD 和 XML,它工作正常。所以请发布您的整个 XSD 和 XML

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="computer">
    <xs:annotation>
      <xs:documentation xml:lang="it-IT">Definizione di un computer</xs:documentation>
      <xs:documentation xml:lang="en-US">Definition of a computer</xs:documentation>
    </xs:annotation>
    <xs:complexType>
      <xs:attribute name="PostInt">
        <xs:simpleType>
          <xs:restriction base="xs:string">
           <xs:length value="6"/>
           <xs:pattern value="\d{0}|\d{6}"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:attribute>
    </xs:complexType>
  </xs:element>
</xs:schema>

XML

<computer PostInt="123456" />

我使用了以下 online validator

关于c# - Xml 验证错误根据其数据类型 'CopyFrom',该值无效 - 枚举约束失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18575144/

相关文章:

php - 使用 ctype_alpha 时允许撇号?

c# - Xamarin.Forms 默认 DatePicker 值?

java - 找不到元素 'mvc:annotation-driven' 的声明

c# - 这仍然是适配器模式吗?

javascript - 测试 xml 中是否有空文本节点

xml - 如何在 xsd 中定义一个元素,该元素在子元素中具有父元素

xml - 根据 XSD 模式验证 XML 并使用 groovy 捕获验证器期望

javascript - RegEx 只允许英文文本,不允许特殊字符

c# - 调试单元测试时执行随机跳转到抛出的异常

c# - .NET : Clarification Request -Processing Threads