C# 将XML反序列化为代码生成的类时出错

C# 将XML反序列化为代码生成的类时出错,c#,asp.net,.net,xml,xsd,C#,Asp.net,.net,Xml,Xsd,我试图从返回XML的web服务中获取数据。web服务还提供XSD文件,我使用XSDTocodefromCodePlex将其转换为类 不幸的是,我遇到了一个错误。我不知道为什么以及如何解决这个问题,所以任何帮助都将不胜感激。错误如下: XML文档(1,2)中存在错误。 innerExection:{”http://www.opengis.net/xls“>不是预期的。”} 来源:System.Xml 下面是我调用以获取XML的示例URL 这将返回以下XML <xls:GeocodeResp

我试图从返回XML的web服务中获取数据。web服务还提供XSD文件,我使用XSDTocodefromCodePlex将其转换为类

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

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

innerExection:{”http://www.opengis.net/xls“>不是预期的。”}

来源:System.Xml

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

这将返回以下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反序列化到的类的一部分

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>

地理编码服务请求
地理编码请求。
用于请求响应中的自由格式地址,而不是结构化地址。
PDOK专有属性,用于指示每个地址必须返回多少个响应。
PDOK专有元素,允许指定地址和应在其中找到地址的边界框。
非结构化的自由格式地址。
地理编码服务响应
地理编码响应。由于地理编码器等的任何解析,返回的地址将是标准化地址ADT。
每个请求地址ADT的响应列表。每个请求的地址可能有1-n个响应(numberOfAddresses)。
为每个地址请求返回的1-n个地址的列表,按精度排序。
这是每个不同请求生成的响应数。在每个地理编码地址内,可能有多个候选地址
PDOKGEOCODADDRESTYPE。找到StreetAddress后,它将作为一个点包含在内。如果只找到一个位置和/或后多边形,则该位置和/或后多边形将作为多多边形和点(多边形的中心)包含在内
地址的地理位置。PDOK专有:如果只找到一个位置和/或后代码,则在多边形中心的点指示。
仅找到位置和/或后代码时使用的PDOK专有元素。

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

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

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


Thanx Stephen,现在像个魔咒一样工作!!只是为了完成。GML标记名来自不同的模式。因此,应按照中的说明编辑代码生成的类
var serializer = new XmlSerializer(typeof(GeocodeResponse));
var serializer = new XmlSerializer(typeof(GeocodeResponse), "http://www.opengis.net/xls");