I'm trying to read an XML file and validate against the schema specified by that file. I will not know the schema's location ahead of time, so I need to use the schema specified by the xml file.
Here's the relevant code (inspired by this answer):
var settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += new ValidationEventHandler(ValidationFailed);
//settings.Schemas.Add("http://www.publishing.org", new XmlTextReader(#"C:\path\to\schema\Book.xsd"));
validatingReader = XmlReader.Create(xmlInputReader, settings);
while (validatingReader.Read()) ;
If I uncomment the settings.Schemas.Add line and comment the settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation out, everything works. I have also tested both the schema and the XML against an external validator.
The event handler message reports "Cannot load the schema for the namespace 'http://www.publishing.org' - Specified argument was out of the range of valid values. Parameter name: baseUri." and it occurs on line 2 (at the root element), followed by "Could not find schema information for the element 'http://www.publishing.org:[each element]'.
My first thought (and still the only thing I know it can be) was that the URI wasn't pointing to the xsd, but I've used 1) A full path via file:///C:\path\to\schema\Book.xsd, 2) A URI relative to the xml file, and 3) A URI relative to the application's current directory. The Visual Studio XML editor has no problem with any of these, but the XmlReader can't seem to find any of them.
Here's a simple schema and an xml instance (my actual schema is more complex, but this fails too):
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.publishing.org" xmlns="http://www.publishing.org"
version="1.0" elementFormDefault="qualified">
<xsd:element name="Book" type="BookType"/>
<xsd:complexType name="BookType">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string" minOccurs="1" maxOccurs="1"/>
<xsd:element name="Author" type="xsd:string" minOccurs="1" maxOccurs="unbounded"/>
<xsd:element name="Date" type="xsd:string" minOccurs="1" maxOccurs="1"/>
<xsd:element name="ISBN" type="xsd:string" minOccurs="1" maxOccurs="1"/>
<xsd:element name="Publisher" type="xsd:string" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
<?xml version="1.0" encoding="UTF-8"?>
<Book xmlns="http://www.publishing.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.publishing.org ../etc/Book.xsd">
<!-- Book.xsd file:///C:\path\to\schema\Book.xsd -->
<Title>Historic Doubts Relative to Napoleon Bonaparte</Title>
<Author>Richard Whately</Author>
<Author>Whately, Richard</Author>
<Date>1849</Date>
<ISBN>1465554777</ISBN>
<Publisher>Warren P. Draper</Publisher>
</Book>
I think everything is correct concerning my namespaces. I have also tried loading through an XmlDocument, but I get the same results. It has to be a problem locating the XSD, right?
I agree it should be a path problem.
I was able to use your code ( and the example you used :) ) .
I tested the validation against a local copy of the xsd, in a file, by setting my local file path inside the xml.
It did nothing when I used your exact xml, and indeed threw the validation error if I changed a tag.
My xsi:schemaLocation looks like:
xsi:schemaLocation="http://www.publishing.org C:\Users\Mike\Desktop\xml_test_files\test.xsd"
Did you try that simple local folder path?
Related
I have one XSD file as below:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:gml="http://www.opengis.net/gml" xmlns:njdot="SomeURL" xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="SomeURL">
<xsd:import namespace="http://www.opengis.net/gml" schemaLocation="SomeURL/feature.xsd"/>
<xsd:complexType name="StudentType">
<xsd:complexContent>
<xsd:extension base="gml:AbstractfeatureType">
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="StudentId" nillable="false" type="xsd:decimal"/>
<xsd:element maxOccurs="1" minOccurs="1" name="class_ID" nillable="false" type="xsd:decimal"/>
<xsd:element maxOccurs="1" minOccurs="1" name="Name" nillable="false" type="xsd:string"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:element name="Student" substitutionGroup="gml:_Feature" type="njdot:StudentType"/>
</xsd:schema>
My requirement is : Read this XSD data into c# on flying and Print them to console.
Currently while compiling XSD, I am getting following error:
Undefined complexType 'http://www.opengis.net/gml:AbstractFeatureType' is used as a base for complex type extension.
Error: Reference to undeclared substitution group affiliation.
I think I am not importing the external schema which is given with
Can anyone suggest what can be the solution or how can I achieve my desired output?
Thanks in advance.
I have TCX exercise files which are written using the schema at https://www8.garmin.com/xmlschemas/TrainingCenterDatabasev2.xsd. I have been using them for years with Java and JAXB. I am trying to write a C# application to do the same thing. It is not going well. I can generate C# classes using xsd.exe as provided by Visual Studio. However, they do not make sense to me and cannot be used to deserialize my TCX files.
The basic structure of TCX files (at least the part in which I am interested) is they have a number of Activities containing a number of Laps containing a number of Tracks containing a number of Trackpoints. The Trackpoints have latitude, longitude, and heart rate as the main items of interest.
The xsd-generated C# classes have an Activity_t[], an ActivityLap_t[], and a Trackpoint_t[][]. There is no Track_t[] and the string Track_t does not appear in the file even though it is in the .xsd, for example in this excerpt for the Lap and Track.
<xsd:complexType name="ActivityLap_t">
<xsd:sequence>
<xsd:element name="TotalTimeSeconds" type="xsd:double"/>
<xsd:element name="DistanceMeters" type="xsd:double"/>
<xsd:element name="MaximumSpeed" type="xsd:double" minOccurs="0"/>
<xsd:element name="Calories" type="xsd:unsignedShort"/>
<xsd:element name="AverageHeartRateBpm" type="HeartRateInBeatsPerMinute_t" minOccurs="0"/>
<xsd:element name="MaximumHeartRateBpm" type="HeartRateInBeatsPerMinute_t" minOccurs="0"/>
<xsd:element name="Intensity" type="Intensity_t"/>
<xsd:element name="Cadence" type="CadenceValue_t" minOccurs="0"/>
<xsd:element name="TriggerMethod" type="TriggerMethod_t"/>
<xsd:element name="Track" type="Track_t" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="Notes" type="xsd:string" minOccurs="0"/>
<xsd:element name="Extensions" type="Extensions_t" minOccurs="0">
<xsd:annotation>
<xsd:documentation>You can extend Training Center by adding your own elements from another schema here.</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="StartTime" type="xsd:dateTime" use="required"/>
</xsd:complexType>
<xsd:complexType name="Track_t">
<xsd:sequence>
<xsd:element name="Trackpoint" type="Trackpoint_t" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
I don't understand how to deal with the [][] nor why it would be generated. And as mentioned it fails to parse.
error CS0030: Cannot convert type
'TrainingCenterDatabaseV2.Trackpoint_t[] to
TrainingCenterDatabaseV2.Trackpoint_t.
(The namespace I used is TrainingCenterDatabaseV2.)
This is the code used to deserialize:
private const string NS_TrainingCenterDatabase_v2 = "http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2";
XmlSerializer xmlSerializer = new XmlSerializer(typeof(TrainingCenterDatabase_t),
NS_TrainingCenterDatabase_v2);
FileStream fs = new FileStream(fileName, FileMode.Open);
XmlReader reader = XmlReader.Create(fs);
TrainingCenterDatabase_t tcx = (TrainingCenterDatabase_t)xmlSerializer.Deserialize(reader);
Not knowing where to go with this I tried the Visual Studio plugin, xsd2code++. This generates sensible C# classes with List<Activity_t>, List<ActivityLap_t>, and List<Trackpoint_t>. It also does not have Track_t, which seems to be somewhat superfluous.
However, it also fails to parse. I believe the problems here have to do with the lack of annotations it generates, compared to the ones generated from xsd.exe. I believe the problem is that it cannot handle the namespaces without those annotations, but I have seen no way to set options to get around that. As stated, I am not experienced with deserialization in C#. For my current purposes I have implemented reading the TCX files using XDocument rather than deserialization. However, I am curious as to why what I tried did not work, especially since xsd.exe has been around for a long time.
Thanks in advance.
This appears to be a bug with the xsd.exe tool itself. I would recommend using LinqToXsd (requires .NET Core 2.1), which is another Microsoft-developed technology for accessing XML data using an XSD; it's also more advanced than xsd.exe and in my quick testing appears to fully handle the above Garmin training center database schema without issue.
Also if you cannot install .NET Core on your machine, you can use this nuget package instead. The .NET Core version requires .NET Core 2.1 to actually generate code, but that generated code that can be used in an app that targets .NET Framework 4.6.2 and above.
I want to read XSD File. But unable to find a proper way to parse XSD file.
<xsd:group name="group_name">
<xsd:annotation>
<xsd:documentation>Some text is here</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element minOccurs="0" name="Element_1" type="string">
<xsd:annotation>
<xsd:documentation>Some text is here</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element minOccurs="0" name="Element_2" type="string">
<xsd:annotation>
<xsd:documentation>Some text is here</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:group>
I want to read each group in my file, a sequence in that group and element in that sequence. And also need to read annotation of each group, I can element.
How can I do that?
XSD File are XML file, so you can read ot using XML parser such as LINQ which is a query to read data from data source, the link bellow is a good source about it:
https://msdn.microsoft.com/en-us/library/bb397933.aspx
So, the first step is to load the xml file then read it by LINQ query
Using the XSD tool included with VS 2013, I receive the following message trying to generate a class from an xsd that contains <xsd:element ref=.../> -
Schema validation warning: The 'http://www.w3.org/2000/09/xmldsig#:KeyName' element is not declared. Line 14, position 8.
Warning: Schema could not be validated. Class generation may fail or may produce incorrect results.
Error: Error generating classes for schema 'test'.
- The element 'http://www.w3.org/2000/09/xmldsig#:Signature' is missing.
This is a cut down xsd that demonstrates the problem:
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema id="test"
targetNamespace="http://tempuri.org/test.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/test.xsd"
xmlns:mstns="http://tempuri.org/test.xsd"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:sig="http://www.w3.org/2000/09/xmldsig#"
>
<xsd:import schemaLocation="xmldsig-core-schema.xsd" namespace="http://www.w3.org/2000/09/xmldsig#" />
<xsd:complexType name="test" >
<xsd:sequence >
<xsd:element ref="sig:Signature" minOccurs="0" maxOccurs="unbounded"></xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:element type="test" name="top"/>
</xsd:schema>
I'm pretty sure the import and namespaces are okay. Resharper and the VS Schema Designer do not complain. I suspect that this is something that the tool just doesn't do.
Any ideas how I can proceed?
It turns out that this has been answered here.
https://stackoverflow.com/a/17278163/2516770
I need to add the imported file to the file list of the xsd command line parameters:
xsd test.xsd xmldsig-core-schema.xsd /c
We have an xsd schema that outlines all the possible data elements in our system, some are simple type elements and some are complex types. We gave this schema to another developer who will try to write up a web service using SOAP. The xsd in a dumb down sample looks like this.
<xs:element name="Element1"> .... </xs:element>
<xs:element name="Element2"> .... </xs:element>
<xs:element name="Element3"> .... </xs:element>
<xs:element name="Element4"> .... </xs:element>
I plan to send our data to him by calling his WebService method. Due to the type of message generated from my system, I will have two type of messages. The first one contains Element1 and Element4 only while the second type may contains Element3, Element4 and Element1. So what are the choice does he have here?
Does he have to create two separate web services and I'll consume them accordingly?
Create one web service and I'll consume that one web service for both of my message types.
In the second opotion, does it generate any schema validation error? if so is there a way to get around it? can I send him a custom SOAP header to indicates the message type I send so he can validates it against specific xsd elements?
I'm relatively new to WebService so if I misunderstood the the basic mechanism of SOAP, XSD and WebService please feel free to point them out or correct any of my above assumptions.
I think that he could make just one webservice.
I'm not very aquainted to XSD as well, but as far as I know, there is a minOccurs property that would make this work with just one webservice. For instance:
<!--The syntax may be incorrect-->
<xsd:complexType name="yourtype">
<xsd:sequence>
<xsd:element name="Element1" minOccurs="1" maxOccurs="1" .../>
<xsd:element name="Element2" minOccurs="0" maxOccurs="1" ..../>
<xsd:element name="Element3" minOccurs="1" maxOccurs="1" ..../>
<xsd:element name="Element4" minOccurs="1" maxOccurs="1" ..../>
</xsd:sequence>
</xsd:complexType>
Look at the minOccurs="0", it means that the Element2 can be ommited by the other programmer. If you want some element to repeat 0 or N times, you can put maxOccurs="unbounded".