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()
    {}
}

完成させるために、XsdToCode を使用してコードを生成した場所に XSD ファイルも含めました。

<?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 に答える