0

我正在尝试从返回 XML 的 Web 服务中获取数据。Web 服务还提供了我使用 XsdToCode fromCodeplex 转换为类的 XSD 文件。

不幸的是,我遇到了一个错误。我不知道为什么以及如何解决这个问题,所以任何帮助将不胜感激。错误如下:

XML 文档 (1, 2) 中存在错误。

内部解释:{“http://www.opengis.net/xls'> 不是预期的。”}

来源:System.Xml

这是我调用以获取 XML 的示例 URL

http://geodata.nationaalgeoregister.nl/geocoder/Geocoder?zoekterm=grote+markt+groningen

这将返回以下 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>

下面是检索 XML 并将其反序列化为对象类的代码

        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;

这是我需要将 XML 反序列化为的类的一部分

public class GeocodeResponse
{

    // ELEMENTS
    [XmlElement("GeocodeResponseList")]
    public GeocodeResponseList GeocodeResponseList { get; set; }

    // CONSTRUCTOR
    public GeocodeResponse()
    {}
}

只是为了完成,我还包括了 XSD 文件,其中我使用 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>

提前感谢您的帮助。

4

1 回答 1

1

您必须将 xml 中的命名空间告知序列化程序,因此将其传递给序列化程序的构造函数。

更改实例化序列化程序的行:

var serializer = new XmlSerializer(typeof(GeocodeResponse));

至:

var serializer = new XmlSerializer(typeof(GeocodeResponse), "http://www.opengis.net/xls");
于 2013-08-05T09:00:42.507 回答