XML 与 XSD 不匹配,并进一步验证属性

本文关键字:进一步 验证 属性 XSD 不匹配 XML | 更新日期: 2023-09-27 18:36:35

XSD schema 说:

Line: 2, Position: 2 "Could not find schema information for the element 'ArrayOfClient'."
Line: 3, Position: 4 "Could not find schema information for the element 'Client'."
Line: 3, Position: 11 "Could not find schema information for the attribute 'ID'."
Line: 3, Position: 27 "Could not find schema information for the attribute 'email'."

XSD 架构:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XMLSchema1"
    targetNamespace="http://tempuri.org/XMLSchema1.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/XMLSchema1.xsd"
    xmlns:mstns="http://tempuri.org/XMLSchema1.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:simpleType name="stringtype">
    <xs:restriction base="xs:string"/>
  </xs:simpleType>
  <xs:complexType name="clientType">
    <xs:attribute name="ID"     type="stringtype" use="required"/>
    <xs:attribute name="email"  type="stringtype" use="required"/>
  </xs:complexType>
  <xs:complexType name="ClientsType">
    <xs:sequence minOccurs="0" maxOccurs="unbounded">
      <xs:element name="Client" type="clientType" />
    </xs:sequence>
  </xs:complexType>
  <xs:element name="ArrayOfClient" type="ClientsType"/>
</xs:schema>

XML文件:

<?xml version="1.0"?>
<ArrayOfClient xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Client ID="00000000" email="user@mail.com" />
</ArrayOfClient>
  1. 这不起作用,xml 文件是使用 .NET C# 中的 XmlSerializer 生成的。
  2. 有没有办法对 ID 属性进行验证,使其始终是 10 位数字和电子邮件......?

编辑:有关我在此处构建XML文件的方式的更多信息。

XML 与 XSD 不匹配,并进一步验证属性

你缺少应该http://tempuri.org/XMLSchema1.xsd的 xmlns,即你的ArrayOfClient应该有一个属性,如 xmlns="http://tempuri.org/XMLSchema1.xsd"

您指向的示例未使用 XML 命名空间。我建议简单地运行 xsd.exe/c your.xsd 并查看生成的类。您将确切地看到您缺少哪些差异。