简体   繁体   English

XDocument.Validate返回的Element具有无效的子元素

[英]XDocument.Validate returning The Element has invalid child element

Getting a really strange issue today while trying to validate XML using XSD. 今天在尝试使用XSD验证XML时遇到了一个非常奇怪的问题。 Looking at the XML I'm providing, it looks correct. 查看我提供的XML,它看起来是正确的。

The error I'm receiving from XDocument.Validate is: The element ' APPOINTMENTS ' had invalid child element ' APPOINTMENT ' 我从XDocument.Validate收到的错误是:元素' APPOINTMENTS '具有无效的子元素' APPOINTMENT '

Here is the XML I'm using: 这是我正在使用的XML:

<PATIENTS>
    <PATIENT>
        <APPOINTMENTS>
            <APPOINTMENT>
                <UserInitials>123</UserInitials>
                <Date>Some Date</Date>
                <ApptTime>14:30</ApptTime>
                <Duration>00:15</Duration>
                <AppointmentStatus>Complete</AppointmentStatus>
                <Notes>Some note</Notes>
                <TreatmentType>Some Appoinment type</TreatmentType>
            </APPOINTMENT>
        </APPOINTMENTS>
    </PATIENT>
</PATIENTS>

And the XSD file I'm validating against: 我要验证的XSD文件:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
 <xs:element name="PATIENTS">
   <xs:complexType>
     <xs:sequence>
       <xs:element name="PATIENT" minOccurs="1">
         <xs:complexType>
           <xs:sequence>
             <xs:element name="APPOINTMENTS" minOccurs="0">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="APPOINTMENT" minOccurs="0">
                    <xs:complexType>
                      <xs:sequence>
                        <xs:element name="UserInitials" type="xs:string" minOccurs="1"></xs:element>
                        <xs:element name="Date" type="xs:string" minOccurs="1"></xs:element>
                        <xs:element name="ApptTime" type="xs:string" minOccurs="1"></xs:element>
                        <xs:element name="Duration" type="xs:string" minOccurs="1"></xs:element>
                        <xs:element name="AppointmentStatus" type="xs:string" minOccurs="1"></xs:element>
                        <xs:element name="LegacyTypeID" type="xs:string" minOccurs="0"></xs:element>
                        <xs:element name="AClinic" minOccurs="0"></xs:element>
                        <xs:element name="Notes" type="xs:string" minOccurs="0"></xs:element>
                        <xs:element name="Info" type="xs:string" minOccurs="0"></xs:element>
                        <xs:element name="TreatmentType" type="xs:string" minOccurs="0" default="Examination"></xs:element>
                      </xs:sequence>
                    </xs:complexType>
                  </xs:element>
                </xs:sequence>
              </xs:complexType>
             </xs:element>
           </xs:sequence>
         </xs:complexType>
       </xs:element>
     </xs:sequence>
   </xs:complexType>
 </xs:element>
</xs:schema>

I don't quite understand what is happening, it looks like the Appointments and Appointments tags are conforming to the XSD file. 我不太了解发生了什么,看来Appointments和Appointments标记符合XSD文件。

The rest of the XML document looks correct, unless there is an issue with the XSD file. 除非XSD文件存在问题,否则XML文档的其余部分看起来都是正确的。

I do have other Elements within my Patient Element that are working fine. 我的患者元素中确实有其他运行良好的元素。

I've worked out the problem, I was actually missing part of the XML to show you. 我已经解决了这个问题,但实际上我缺少向您展示的XML部分。

I should have included the following: 我应该包括以下内容:

<?xml version="1.0" encoding="utf-8" ?>
<PATIENTS>
  <PATIENT>
    <APPOINTMENTS>
      <APPOINTMENT>
        <UserInitials>123</UserInitials>
        <Date>Some Date</Date>
        <ApptTime>14:30</ApptTime>
        <Duration>00:15</Duration>
        <AppointmentStatus>Complete</AppointmentStatus>
        <Notes>Some note</Notes>
        <TreatmentType>Some Appoinment type</TreatmentType>
      </APPOINTMENT>
      <APPOINTMENT>
        <UserInitials>123</UserInitials>
        <Date>Some Date</Date>
        <ApptTime>14:30</ApptTime>
        <Duration>00:15</Duration>
        <AppointmentStatus>Complete</AppointmentStatus>
        <Notes>Some note</Notes>
        <TreatmentType>Some Appoinment type</TreatmentType>
      </APPOINTMENT>
      <APPOINTMENT>
        <UserInitials>123</UserInitials>
        <Date>Some Date</Date>
        <ApptTime>14:30</ApptTime>
        <Duration>00:15</Duration>
        <AppointmentStatus>Complete</AppointmentStatus>
        <Notes>Some note</Notes>
        <TreatmentType>Some Appoinment type</TreatmentType>
      </APPOINTMENT>
    </APPOINTMENTS>
  </PATIENT>
</PATIENTS>

I Actually have multiple records within the APPOINTMENT Element, which would require the XSD file to have the following on the APPOINTMENT element: 我实际上在APPOINTMENT元素内有多个记录,这将要求XSD文件在APPOINTMENT元素上具有以下内容:

<xs:element name="APPOINTMENT" minOccurs="0" maxOccurs="unbounded">

I was missing the maxOccurs="unbounded" attribute 我缺少maxOccurs="unbounded"属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM