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

本文关键字:出错 代码生成 XML 反序列化 | 更新日期: 2023-09-27 18:26:30

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

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

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>

提前感谢任何帮助。

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

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

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

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

至:

var serializer = new XmlSerializer(typeof(GeocodeResponse), "http://www.opengis.net/xls");