简体   繁体   中英

XDocument.Validate returning The Element has invalid child element

Getting a really strange issue today while trying to validate XML using XSD. Looking at the XML I'm providing, it looks correct.

The error I'm receiving from XDocument.Validate is: The element ' APPOINTMENTS ' had invalid child element ' APPOINTMENT '

Here is the XML I'm using:

<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:

<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.

The rest of the XML document looks correct, unless there is an issue with the XSD file.

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.

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:

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

I was missing the maxOccurs="unbounded" attribute

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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