System.Data.DataSet is not Completely Reading the XML File - c#

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

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

When validate XSD against XML getting an error?

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>

XML auto generation issue

This is what my xsd looks like. Air and Car extend Segment.
<xs:element name="PNR" type="PNR" />
<xs:element minOccurs="0" maxOccurs="1" name="Segments" >
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="Segment" type="Segment"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="Air">
<xs:complexContent>
<xs:extension base="Segment">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" name="Departure" type="AirportInfo" />
<xs:element minOccurs="0" maxOccurs="1" name="Arrival" type="AirportInfo" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="Car">
<xs:complexContent>
<xs:extension base="Segment">
<xs:sequence>
<xs:element name="PickUp" type="AddressInfo" minOccurs="0" maxOccurs="1"/>
<xs:element name="DropOff" type="AddressInfo" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
I want an output that looks like this:
<Segments>
<Segment xsi:type="Air">
<Departure></Departure>
<Arrival></Arrival>
</Segment>
<Segment xsi:type="Car">
<PickUp></PickUp>
<DropOff></DropOff>
</Segment>
</Segments>
But when I auto-generate XML from my XSD using a tool such as xmlspy or a .net library (or whatever), I get this (Air generated multi times w/o Car).
<Segments>
<Segment xsi:type="Air">
<Departure></Departure>
<Arrival></Arrival>
</Segment>
<Segment xsi:type="Air">
<Departure></Departure>
<Arrival></Arrival>
</Segment>
</Segments>
Is there something wrong with my XSD?
Here's a technique I once used to generate XML from a set of schemas. Although it doesn't look like the right class for the job, take a look at the XmlSchemaValidator class. The trick is that you can ask it what is valid for the document you're validating at that point in the validation. You can then generate the XML that is valid at that point in the document, and validate the same XML, in order to update the XmlSchemaValidator. You can then ask it again what is valid at that point in the document, etc.
When you get to an abstract type, you'll have to know to generate one of each possible derived type.

WCF Web HTTP Service Help Page

I have created a simple Northwind's Product REST Web Service in WCF at /Northwind/Product. I have also enabled WCF Web HTTP Service Help Page on my service, which is at /Northwind/Product/help. I have a "GET" operation and its help page is located at: /Northwind/Product/help/operations/Get, which is your standard WCF help page that displays the Xml body, Json body, Xml Schema and Additional Xml Schemas. Pretty straight forward, right? Okay now, on to the fun stuff...
I am interested in the Xml Schema section, which is:
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="Product">
<xs:sequence>
<xs:element minOccurs="0" name="CategoryID" nillable="true" type="xs:int" />
<xs:element minOccurs="0" name="Discontinued" type="xs:boolean" />
<xs:element minOccurs="0" name="ProductID" type="xs:int" />
<xs:element minOccurs="0" name="ProductName" nillable="true" type="xs:string" />
<xs:element minOccurs="0" name="QuantityPerUnit" nillable="true" type="xs:string" />
<xs:element minOccurs="0" name="ReorderLevel" nillable="true" type="xs:short" />
<xs:element minOccurs="0" name="SupplierID" nillable="true" type="xs:int" />
<xs:element minOccurs="0" name="UnitPrice" nillable="true" type="xs:decimal" />
<xs:element minOccurs="0" name="UnitsInStock" nillable="true" type="xs:short" />
<xs:element minOccurs="0" name="UnitsOnOrder" nillable="true" type="xs:short" />
</xs:sequence>
</xs:complexType>
<xs:element name="Product" nillable="true" type="Product" />
</xs:schema>
I am interested in it because of the datatypes. I want to know the datatypes of the elements. Now, I understand that's not the fundamentals of REST. However, I don't want SOAP objects here. I want to keep my services simple and loosely typed and yet still be aware of their datatypes when needed.
My question is, how can I expose just this particular section of the help file? If I cannot do that, what are my other options for achieving what I am trying to do here?
Maybe you could try with OData, as I see it it is in between of REST and SOAP.
http://msdn.microsoft.com/es-es/library/cc668794.aspx
I do not think that it is possible to be both loosly typed and a the same time know the types behind the fields.
You could send everything as a string, and then throw an exception if conversion is not possible.
There does not appear to be a tag in the xs:element tag that could be used for help infomation, http://www.w3schools.com/schema/el_element.asp

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