Reputation: 132
I’m trying to get data from a web service that returns XML. The web service also provide XSD file that I converted to a class using XsdToCode fromCodeplex.
Unfortunately I’m getting an error. I don’t know why and how to solve this so any help would be appreciated. The error is as followed:
There is an error in XML document (1, 2).
Innerexeption: {"http://www.opengis.net/xls'> was not expected."}
Source: System.Xml
Here’s the sample URL that I’m calling to get the XML
http://geodata.nationaalgeoregister.nl/geocoder/Geocoder?zoekterm=grote+markt+groningen
This returns the Following XML
<xls:GeocodeResponse xmlns:xls="http://www.opengis.net/xls" xmlns:gml="http://www.opengis.net/gml">
<xls:GeocodeResponseList numberOfGeocodedAddresses="1">
<xls:GeocodedAddress>
<gml:Point srsName="EPSG:28992">
<gml:pos dimension="2">233818.478 582036.58</gml:pos>
</gml:Point>
<xls:Address countryCode="NL">
<xls:StreetAddress>
<xls:Street>Grote Markt</xls:Street>
</xls:StreetAddress>
<xls:Place type="MunicipalitySubdivision">Groningen</xls:Place>
<xls:Place type="Municipality">Groningen</xls:Place>
<xls:Place type="CountrySubdivision">Groningen</xls:Place>
</xls:Address>
</xls:GeocodedAddress>
</xls:GeocodeResponseList>
</xls:GeocodeResponse>
Below is the code to retrieve the XML and deserialize it to an object class
var request = WebRequest.Create(url);
var response = (HttpWebResponse)request.GetResponse();
var serializer = new XmlSerializer(typeof(GeocodeResponse));
var i = (GeocodeResponse)serializer.Deserialize(new StreamReader(response.GetResponseStream()));
return i;
Here is a part is the class that i need the XML deserialized into
public class GeocodeResponse
{
// ELEMENTS
[XmlElement("GeocodeResponseList")]
public GeocodeResponseList GeocodeResponseList { get; set; }
// CONSTRUCTOR
public GeocodeResponse()
{}
}
Just for completion I also included the XSD file on where I generated the code from using XsdToCode.
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSpy v2011 (x64) (http://www.altova.com) by Jasper de Barbanson (E-id! Internet Strategies) -->
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:gml="http://www.opengis.net/gml" xmlns:xls="http://www.opengis.net/xls" targetNamespace="http://www.opengis.net/xls" elementFormDefault="qualified" version="1.2.1">
<import namespace="http://www.opengis.net/gml" schemaLocation="gml4xls.xsd"/>
<include schemaLocation="XLS.xsd"/>
<!-- ====================================================== -->
<element name="GeocodeRequest" type="xls:GeocodeRequestType" substitutionGroup="xls:_RequestParameters">
<annotation>
<documentation>Geocode Service Request</documentation>
</annotation>
</element>
<!-- ====================================================== -->
<complexType name="GeocodeRequestType">
<annotation>
<documentation>Geocode Request. </documentation>
</annotation>
<complexContent>
<extension base="xls:AbstractRequestParametersType">
<sequence>
<element name="Address" type="xls:PdokAddressType" maxOccurs="unbounded"/>
</sequence>
<attribute name="returnFreeForm" type="boolean" use="optional" default="false">
<annotation>
<documentation>Use to request freeform addresses in the response, as opposed to structured adddresses.</documentation>
</annotation>
</attribute>
<attribute name="maxResponseItemsPerAddress" type="nonNegativeInteger" use="optional" default="10">
<annotation>
<documentation>PDOK proprietary attribute used to indicate how many responses must be returned per address.</documentation>
</annotation>
</attribute>
</extension>
</complexContent>
</complexType>
<!-- ====================================================== -->
<complexType name="PdokAddressType">
<annotation>
<documentation>PDOK proprietary element allowing to specify an address and the bounding box in which it should be found.</documentation>
</annotation>
<complexContent>
<extension base="xls:AbstractAddressType">
<sequence>
<choice>
<element name="freeFormAddress" type="string">
<annotation>
<documentation>An unstructured free form address.</documentation>
</annotation>
</element>
<sequence>
<element ref="xls:StreetAddress"/>
<element ref="xls:Place" minOccurs="0" maxOccurs="unbounded"/>
<element ref="xls:PostalCode" minOccurs="0"/>
</sequence>
</choice>
</sequence>
</extension>
</complexContent>
</complexType>
<!-- ====================================================== -->
<element name="GeocodeResponse" type="xls:GeocodeResponseType" substitutionGroup="xls:_ResponseParameters">
<annotation>
<documentation>Geocode Service Response</documentation>
</annotation>
</element>
<!-- ====================================================== -->
<complexType name="GeocodeResponseType">
<annotation>
<documentation>GeocodeResponse. The addresses returned will be normalized Address ADTs as a result of any parsing by the geocoder, etc.</documentation>
</annotation>
<complexContent>
<extension base="xls:AbstractResponseParametersType">
<sequence>
<element ref="xls:GeocodeResponseList" maxOccurs="unbounded"/>
</sequence>
</extension>
</complexContent>
</complexType>
<!-- ====================================================== -->
<element name="GeocodeResponseList" type="xls:GeocodeResponseListType">
<annotation>
<documentation>The list of responses for each of the requested Address ADTs. Each requested address may have 1-n responses (numberOfAddresses). </documentation>
</annotation>
</element>
<!-- ====================================================== -->
<complexType name="GeocodeResponseListType">
<sequence>
<element name="GeocodedAddress" type="xls:PdokGeocodedAddressType" maxOccurs="unbounded">
<annotation>
<documentation>The list of 1-n addresses that are returned for each Address request, sorted by Accuracy.</documentation>
</annotation>
</element>
</sequence>
<attribute name="numberOfGeocodedAddresses" type="nonNegativeInteger" use="required">
<annotation>
<documentation>This is the number of responses generated per the different requests. Within each geocoded address tit's possible to have multiple candidates</documentation>
</annotation>
</attribute>
</complexType>
<!-- ====================================================== -->
<complexType name="PdokGeocodedAddressType">
<annotation>
<documentation>PdokGeocodedAddressType. When an StreetAddress is found it will be included as a Point. If only a Place and/or Postalcode is found it will be included as a MultiPolygon and as a Point (the centre of the polygon)</documentation>
</annotation>
<sequence>
<element ref="gml:Point">
<annotation>
<documentation>Geo location of the address. PDOK proprietary: Point indicatin the center of the polygon if only a Place and/or Postalcode is found.</documentation>
</annotation>
</element>
<element ref="gml:MultiPolygon" minOccurs="0">
<annotation>
<documentation>PDOK proprietary element used when only a Place and/or Postalcode is found.</documentation>
</annotation>
</element>
<element ref="xls:Address"/>
<element ref="xls:GeocodeMatchCode" minOccurs="0"/>
</sequence>
</complexType>
<!-- ====================================================== -->
</schema>
Thanx in advance for any help.
Upvotes: 0
Views: 672
Reputation: 2047
You have to inform the serializer about the namespace in the xml, so pass it into the constructor of the serializer.
Change the line that instantiates the serializer:
var serializer = new XmlSerializer(typeof(GeocodeResponse));
to:
var serializer = new XmlSerializer(typeof(GeocodeResponse), "http://www.opengis.net/xls");
Upvotes: 1