【问题标题】:C# SOAP client unable to deserialize response ("There is an error in XML document")C# SOAP 客户端无法反序列化响应(“XML 文档中存在错误”)
【发布时间】:2012-12-14 12:35:13
【问题描述】:

我在我的项目中添加了对 Web 服务的引用。我正在使用生成的代码来调用 Web 服务的方法。其中一种方法返回以下结构:

    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:typens="urn:AllegroWebApi" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
       <SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
          <typens:doGetCatsDataResponse>
             <cats-list xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="typens:CatInfoType[24051]">
                <item xsi:type="typens:CatInfoType">
                   <cat-id xsi:type="xsd:int">26013</cat-id>
                   <cat-name xsi:type="xsd:string">Antyki i Sztuka</cat-name>
                   <cat-parent xsi:type="xsd:int">0</cat-parent>
                   <cat-position xsi:type="xsd:int">0</cat-position>
                   <cat-is-product-catalogue-enabled xsi:type="xsd:int">0</cat-is-product-catalogue-enabled>
                </item>
            ....

            </item>
            <item xsi:type="typens:CatInfoType">
               <cat-id xsi:type="xsd:int">124895</cat-id>
               <cat-name xsi:type="xsd:string">Pozostałe</cat-name>
               <cat-parent xsi:type="xsd:int">124883</cat-parent>
               <cat-position xsi:type="xsd:int">5</cat-position>
               <cat-is-product-catalogue-enabled xsi:type="xsd:int">0</cat-is-product-catalogue-enabled>
            </item>
            <item xsi:type="typens:CatInfoType">
               <cat-id xsi:type="xsd:int">124894</cat-id>
               <cat-name xsi:type="xsd:string">Teleskopy</cat-name>
               <cat-parent xsi:type="xsd:int">124883</cat-parent>
               <cat-position xsi:type="xsd:int">6</cat-position>
               <cat-is-product-catalogue-enabled xsi:type="xsd:int">0</cat-is-product-catalogue-enabled>
            </item>     // Line 168361   <--------------- HERE
         </cats-list>
         <ver-key xsi:type="xsd:long">91632766</ver-key>
         <ver-str xsi:type="xsd:string">1.1.47</ver-str>
      </typens:doGetCatsDataResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

负责反序列化的生成代码会抛出以下异常:

“System.InvalidOperationException”类型的未处理异常 System.Xml.dll 中发生附加信息:有错误 在 XML 文档中 (168361, 13)。

我标记了代码示例中异常中提到的行。我想这与它是最后一个 item 对象的结束标记有关。我不知道它为什么会失败。 XML 格式正确,似乎不包含任何不受支持的字符。

【问题讨论】:

  • 你可以先尝试验证你的xml,网上有几个解决方案,比如w3schools.com/dom/dom_validate.asp。或者在 Firefox 或 IE 中打开它,如果出现异常,它们应该标记确切的行。

标签: c# xml web-services serialization soap


【解决方案1】:

只是猜测,但请检查您的数据,可能是其中一个字段带有“”,从而导致 xml 文档的格式不正确。

【讨论】:

  • 哎呀应该学会阅读整个问题,你已经试过了:-)
  • 事实上就是这样。我使用了一个简单的正则表达式并找到了一个包含“
【解决方案2】:

我看到您连续有两个结束项目标签。当然,您可能会排除某些 XML 文件。尽管如此,连续两个结束标签肯定会导致该错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 2016-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多