XML 验证 C# - 复杂类型

发布于 2025-01-07 01:42:30 字数 3084 浏览 1 评论 0原文

我正在从 XFA 静态表单中提取一些 xml。 下面是一个示例:

<xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
<xfa:data>
<frmMain>
<InspectionDate>19/02/2012</InspectionDate>
<ENID>111114567</ENID>
<EmployeeNumber>1234</EmployeeNumber>
<GroundType>
   <value>Tarmac</value>
   <value>Concrete</value>
</GroundType>
<Width>800</Width>
<Height>900</Height>
<OtherDetails>Corssing of x road and y street</OtherDetails>
</frmMain>
</xfa:data>
</xfa:datasets>

我使用 Windows SDK 7.0 实用程序 Xsd.exe 生成架构,以便我可以根据该 XML 进行验证 如下:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="datasets" targetNamespace="http://www.xfa.org/schema/xfa-data/1.0/" xmlns:mstns="http://www.xfa.org/schema/xfa-data/1.0/" xmlns="http://www.xfa.org/schema/xfa-data/1.0/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified">
  <xs:attribute name="dataNode" msdata:Prefix="xfa" type="xs:string" />
  <xs:element name="datasets" msdata:IsDataSet="true" msdata:Locale="en-US" msdata:Prefix="xfa">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="data" msdata:Prefix="xfa">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="frmMain" form="unqualified">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="InspectionDate" form="unqualified" type="xs:string" minOccurs="0" />
                    <xs:element name="ENID" form="unqualified" type="xs:string" minOccurs="0" />
                    <xs:element name="EmployeeNumber" form="unqualified" type="xs:string" minOccurs="0" />
                    <xs:element name="Width" form="unqualified" type="xs:string" minOccurs="0" />
                    <xs:element name="Height" form="unqualified" type="xs:string" minOccurs="0" />
                    <xs:element name="OtherDetails" form="unqualified" type="xs:string" minOccurs="0" />
                    <xs:element name="GroundType" form="unqualified" minOccurs="0" maxOccurs="unbounded">
                      <xs:complexType>
                        <xs:attribute ref="mstns:dataNode" />
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

我使用 XmlReaderSettings 和 XmlReader 来读取 Xml 并针对 Xsd 进行验证。只要我没有复杂的类型,一切都可以。但在这种情况下我有。

可以做什么?

我得到的错误是:

元素“GroundType”不能包含子元素“value”,因为父元素的内容模型为空。

I'm extracting some xml from an XFA static form.
Here is a sample:

<xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
<xfa:data>
<frmMain>
<InspectionDate>19/02/2012</InspectionDate>
<ENID>111114567</ENID>
<EmployeeNumber>1234</EmployeeNumber>
<GroundType>
   <value>Tarmac</value>
   <value>Concrete</value>
</GroundType>
<Width>800</Width>
<Height>900</Height>
<OtherDetails>Corssing of x road and y street</OtherDetails>
</frmMain>
</xfa:data>
</xfa:datasets>

I use Windows SDK 7.0 utility Xsd.exe to generate a schema so I can validate that XML against
Here it is:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="datasets" targetNamespace="http://www.xfa.org/schema/xfa-data/1.0/" xmlns:mstns="http://www.xfa.org/schema/xfa-data/1.0/" xmlns="http://www.xfa.org/schema/xfa-data/1.0/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified">
  <xs:attribute name="dataNode" msdata:Prefix="xfa" type="xs:string" />
  <xs:element name="datasets" msdata:IsDataSet="true" msdata:Locale="en-US" msdata:Prefix="xfa">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="data" msdata:Prefix="xfa">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="frmMain" form="unqualified">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="InspectionDate" form="unqualified" type="xs:string" minOccurs="0" />
                    <xs:element name="ENID" form="unqualified" type="xs:string" minOccurs="0" />
                    <xs:element name="EmployeeNumber" form="unqualified" type="xs:string" minOccurs="0" />
                    <xs:element name="Width" form="unqualified" type="xs:string" minOccurs="0" />
                    <xs:element name="Height" form="unqualified" type="xs:string" minOccurs="0" />
                    <xs:element name="OtherDetails" form="unqualified" type="xs:string" minOccurs="0" />
                    <xs:element name="GroundType" form="unqualified" minOccurs="0" maxOccurs="unbounded">
                      <xs:complexType>
                        <xs:attribute ref="mstns:dataNode" />
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

I use XmlReaderSettings with a XmlReader to read and validate the Xml against the Xsd. Everything is ok as long as I do not have a complex type. But in this case I have.

What can be done?

And the Error I'm getting is:

The element 'GroundType' cannot contain child element 'value' because the parent element's content model is empty.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌逼全场 2025-01-14 01:42:30

您的 GroundType 定义应如下所示:

<xs:element name="GroundType" form="unqualified" minOccurs="0" maxOccurs="unbounded">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="value" form="unqualified" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
        <xs:attribute ref="mstns:dataNode"/>
    </xs:complexType>
</xs:element>

Your GroundType definition should look like this:

<xs:element name="GroundType" form="unqualified" minOccurs="0" maxOccurs="unbounded">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="value" form="unqualified" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
        <xs:attribute ref="mstns:dataNode"/>
    </xs:complexType>
</xs:element>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文