When validate XSD against XML getting an error? - c#

I'm trying to validate XSD aginst XML but getting an error
The element 'Table' has incomplete content. List of possible elements expected: 'IP21TAG'.
XML:
<NewDataSet>
<Table>
<SITE>VMD</SITE>
<TANK>65-12-392</TANK>
<SERVICE>HZLPG</SERVICE>
</Table>
<Table>
<SITE>VMD</SITE>
<TANK>65-12-392</TANK>
<SERVICE>HZLPG</SERVICE>
<IP21TAG>BC-BBH-OS-4LI21392</IP21TAG>
</Table>
</NewDataSet>
XSD:
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="NewDataSet">
<xs:complexType>
<xs:sequence>
<xs:element name="Table">
<xs:complexType>
<xs:sequence>
<xs:element name="SITE" type="xs:string" />
<xs:element name="PLANT" type="xs:string" />
<xs:element name="TANK" type="xs:string" />
<xs:element name="SERVICE" type="xs:string" />
<xs:element name="IP21TAG" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Can anyone help me how to solve this?
Thanks in advance.

Obviously, solution is to set minOccurs="0" on elements that are optional.
However, error message in Visual Studio displayes even optional elements in the "possible element name" list, so it is not obvious if you miss minOccurs on an element.
My problem was that ONE element missed the minOccurs="0", and error message listed ALL, approximately 100, elements...

You omit minOccurs in <xs:element> node of your schema, its default is one (see specifications) then if you don't specify that node your XML won't be validated against that schema.
If that node is optional simply change your XSD to reflect that. Here I changed just IP21TAG and PLANT (because they're both not present in your example XML but if others are optional too you should change them accordingly):
<xs:element name="IP21TAG" type="xs:string" minOccurs="0"/>
<xs:element name="PLANT" type="xs:string" minOccurs="0" />
If that element is not optional then to be wrong is your XML, you may - for example - provide an empty string instead of a missing node:
<NewDataSet>
<Table>
<SITE>VMD</SITE>
<TANK>65-12-392</TANK>
<SERVICE>HZLPG</SERVICE>
<IP21TAG></IP21TAG>
<PLANT></PLANT>
</Table>
</NewDataSet>

Related

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

System.Data.DataSet is not Completely Reading the XML File

I am using NDbunit to unit test the functionality of my methods and database. For NDbunit to work, it firsts loads an xml schema file (.xsd) and then reads in the xml file with all the data that will be populated into the database. Here is my xml schema file MessageDS.xsd:
<xs:schema id="MessageDS"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
xmlns:msprop="urn:schemas-microsoft-com:xml-msprop"
targetNamespace="http://tempuri.org/MessageDS.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/MessageDS.xsd"
xmlns:mstns="http://tempuri.org/MessageDS.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:element name="MessageDS" msdata:IsDataSet="true" msdata:UseCurrentLocalexmlns="true" msprop:Generator_MessageDSName="MessageDS" msprop:Generator_DataSetName="MessageDS">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="MESSAGE">
<xs:complexType>
<xs:sequence>
<xs:element name="CREATED_AT" type="xs:dateTime" />
<xs:element name="SUBJECT" type="xs:string" />
<xs:element name="MESSAGE" type="xs:string" />
<xs:element name="FROM" type="xs:string" />
<xs:element name="TO" type="xs:string" />
<xs:element name="TO_EMAIL" type="xs:string" />
<xs:element name="EMAIL_SENT_AT" type="xs:dateTime" />
</xs:sequence>
<xs:attribute name="ID" type="xs:int" use="required" />
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
Here is my xml data file Message.xml:
<MessageDS xlmns="http://tempuri.org/MessageDS.xsd">
<MESSAGE ID="1">
<FROM>Test User 2</FROM>
<TO>Test User 1</TO>
<MESSAGE>
</MessageDS>
Initially I was just using the dll references for NDbunit but eventually I downloaded the source code and started debugging through the problems. I noticed that after the xml schema file is read in the xml file is not being properly loaded into the dataset (System.Data.DataSet). The only xml that is written in is:
<MessageDS xmlns="http://tempuri.org/MessageDS.xsd" />
For some reason my MESSAGE objects are not being read into the xml file. I'm not sure if this is because my xml file is not properly created according to the xml schema file or something else is the cause. I tried to follow the examples on the https://code.google.com/p/ndbunit/wiki/QuickStartGuide for NDbunit and I also looked at the xml files in the testing files for NDbunit.
To start with, "xlmns" is misspelled:
<MessageDS xlmns="http://tempuri.org/MessageDS.xsd">

Modify xsd not to look for specific order of tags

I need to fix the error message below:
Error occured while validating xmlThe element 'UserFields' has invalid child element 'LastApproverID'. List of possible elements expected: 'FirtApproverID'.
Below is my xsd for validating the xml schema :
<xs:element name="UserFields" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="FirtApproverID" type="xs:string" minOccurs="1" />
<xs:element name="LastApproverID" type="xs:string" minOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:element>
The order of the firstapproverid and lastapproverid does not matter but the tag need to occur atleast once.
i.e it can appear
<UserFields>
<LastApproverID>123</LastApproverID>
<FirtApproverID>456</FirtApproverID>
</UserFields>
OR
<UserFields>
<FirtApproverID>456</FirtApproverID>
<LastApproverID>123</LastApproverID>
</UserFields>
The <xs:all> indicator can solve your problem.
See here for answer
Please, improve your Google skills.

How to specify a child element in XML schema with a name but any content?

I am trying to write some XML schema code to specify that a particular element 'abc' may have a child element with name 'xyz', and that element may have any attributes, and any child elements.
At the moment I have this:
<xs:element name="abc">
<xs:complexType>
<xs:sequence>
<xs:element name="xyz">
<xs:complexType>
<xs:sequence>
<xs:any/>
</xs:sequence>
<xs:anyAttribute/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
But when I validate my XML against the schema, I get validation failures complaining about the child elements of the xyz element.
Your xs:any doesn't have any information attached to it, so it is looking for schema defined elements. If you want to be slack in your interpretation of the sub-elements, try this:
<xs:element name="abc">
<xs:complexType>
<xs:sequence>
<xs:element name="xyz">
<xs:complexType>
<xs:sequence>
<xs:any processContents="lax" />
</xs:sequence>
<xs:anyAttribute/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
This should get you past validation. If you have any expectation on the well-formedness of xyz's content, you can include a namespace using the namespace attribute for xs:any, and pull in another schema for that information.
Good luck, and I hope this helps!

XML Schema not working with Web Service Software Factory

I am trying to create an XML schema to use with the Web Service Software Factory. It's a fairly simple schema that is just a group of person objects. The (simplified) schema file looks like:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Persons" type="PersonsType" />
<xs:complexType name="PersonsType">
<xs:sequence>
<xs:element name="Person" type="PersonType" minOccurs="0"
maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="PersonType">
<xs:all>
<xs:element name="PersonName" type="xs:string" />
<xs:element name="PersonAge" type="xs:integer" />
</xs:all>
</xs:complexType>
</xs:schema>
It's a simple collection of person elements with a parent element called Persons.
When I try to validate my .serviceContract file I get the error 'The file name 'Persons.xsd' is not compliant with the DataContactSerializer'.
Does anybody know how to fix this schema so that it will work with the Web Service Software Factory? And for bonus points, the next structure that I have to worry about will be a recursive list of corporations. Any suggestions about how to make recursive schemas that work with WSSF would be appreciated as well.
Did you already try to avoid named types?
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Persons">
<xs:complexType>
<xs:sequence>
<xs:element name="Person" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:all>
<xs:element name="PersonName" type="xs:string" />
<xs:element name="PersonAge" type="xs:integer" />
</xs:all>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
Also you may try to change <xs:all> to <sequence> in your <Person>.

Categories

Resources