【问题标题】:Importing Sales Orders from Magento Using C# (salesOrderList function): "There is an error in XML document (2, 372)"使用 C#(salesOrderList 函数)从 Magento 导入销售订单:“XML 文档中存在错误 (2, 372)”
【发布时间】:2012-01-05 10:40:34
【问题描述】:

注意:此问题最初是 Magento SOAP API V2 with C#: Issue with Stores that Require HTTP Authentication 的一部分,但我已将其中一部分作为新问题移至此处,因为该帖子中已经回答了一部分。

问题: 我已经构建了一个工具来从客户商店 (magento) 导入销售订单,以便集成到我们的旧订单处理系统中。我有五家商店可以进口。三个工作正常。其中两个有以下问题:

  • 执行salesOrderList() 调用时抛出以下异常:

    System.Xml.dll 中发生了“System.InvalidOperationException”类型的第一次机会异常

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

    登录成功,我可以枚举属性值等。 但我无法使用任何功能运行 salesOrderList() 函数 过滤器(还有null)。如果有人有经验就好了 有了这个,请给我指出正确的方向。


更新:调用调用如下:

salesOrderEntity[] soe = mservice.salesOrderList(mlogin, objSalesOrderFilterSet);

例外是,XML 文档中存在错误 (2, 372)。
InnerException 是,无法识别指定类型:name='salesOrderListEntity', namespace='urn:Magento', at .

Fiddler 为 salesOrderList() 调用显示了以下请求并返回:
请求:

POST /api/v2_soap/index HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 4.0.30319.239)
VsDebuggerCausalityData: uIDPoz6RG9JzwkdBniF95/gqmAcAAAAAhgJHnbvB1UOTE1y4R1Iq5VGLcSLUxTNDg57BO/4OizgACQAA
Content-Type: text/xml; charset=utf-8
SOAPAction: "urn:Mage_Api_Model_Server_V2_HandlerAction"
Host: www.contoso.com
Content-Length: 1753
Expect: 100-continue

返回:

    POST http://www.contoso.com/api/v2_soap/index HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 4.0.30319.239)
VsDebuggerCausalityData: uIDPoz6RG9JzwkdBniF95/gqmAcAAAAAhgJHnbvB1UOTE1y4R1Iq5VGLcSLUxTNDg57BO/4OizgACQAA
Content-Type: text/xml; charset=utf-8
SOAPAction: "urn:Mage_Api_Model_Server_V2_HandlerAction"
Host: www.contoso.com
Content-Length: 1753
Expect: 100-continue

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:Magento" xmlns:types="urn:Magento/encodedTypes" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><tns:salesOrderList><sessionId xsi:type="xsd:string">2f9c5bdc898fd92c1f61981147ee5495</sessionId><filters href="#id1" /></tns:salesOrderList><tns:filters id="id1" xsi:type="tns:filters"><complex_filter href="#id2" /></tns:filters><soapenc:Array id="id2" soapenc:arrayType="tns:complexFilter[3]"><Item href="#id3" /><Item href="#id4" /><Item href="#id5" /></soapenc:Array><tns:complexFilter id="id3" xsi:type="tns:complexFilter"><key xsi:type="xsd:string">created_at</key><value href="#id6" /></tns:complexFilter><tns:complexFilter id="id4" xsi:type="tns:complexFilter"><key xsi:type="xsd:string">created_at</key><value href="#id7" /></tns:complexFilter><tns:complexFilter id="id5" xsi:type="tns:complexFilter"><key xsi:type="xsd:string">status</key><value href="#id8" /></tns:complexFilter><tns:associativeEntity id="id6" xsi:type="tns:associativeEntity"><key xsi:type="xsd:string">from</key><value xsi:type="xsd:string">2011-12-28 00:00:00</value></tns:associativeEntity><tns:associativeEntity id="id7" xsi:type="tns:associativeEntity"><key xsi:type="xsd:string">to</key><value xsi:type="xsd:string">2011-12-28 23:59:59</value></tns:associativeEntity><tns:associativeEntity id="id8" xsi:type="tns:associativeEntity"><key xsi:type="xsd:string">in</key><value xsi:type="xsd:string">processing</value></tns:associativeEntity></soap:Body></soap:Envelope>
HTTP/1.1 200 OK
Date: Thu, 05 Jan 2012 12:11:19 GMT
Server: Apache/2.2.20 (Unix) mod_ssl/2.2.20 OpenSSL/1.0.0-fips mod_bwlimited/1.4 mod_fcgid/2.3.6
X-Powered-By: PHP/5.3.6
Content-Length: 6302
Content-Type: text/xml; charset=UTF-8

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:Magento" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:salesOrderListResponse><result SOAP-ENC:arrayType="ns1:salesOrderListEntity[1]" xsi:type="ns1:salesOrderListEntityArray"><item xsi:type="ns1:salesOrderListEntity"><increment_id xsi:type="xsd:string">100001306</increment_id><store_id 
...<more data>...
xsi:type="xsd:string">John</firstname><lastname xsi:type="xsd:string">Doe</lastname><telephone xsi:type="xsd:string">999-999-9999 ext. 3333</telephone><postcode xsi:type="xsd:string">11111</postcode></item></result></ns1:salesOrderListResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>

【问题讨论】:

  • 您可以从 fiddler 复制/粘贴文档以便我们查看输出吗?没有看到输出将很难帮助您...
  • 是的,我一定会这样做的。同时,您能否在此处发布您之前的提琴手建议作为答案,以便如果这项工作成功,我可以“接受”它作为答案?谢谢。
  • 没有。给我们文档以便我们更好地帮助您,只是告诉您检查fiddler 不应该是一个可接受的答案...只需在localhost 中打开 fiddler 和您的项目,然后拨打电话并查看 HTTP 中发生了什么协议,检查你在问什么,你收到什么......
  • 感谢您的指导。我已经从提琴手粘贴了文件,减去了客户数据。它似乎确实正确地返回了数据,但调用抛出了所描述的 XML 错误异常,使得所有数据都无法通过 Magento SOAP V2 对象模型访问。
  • 其他商店的 API 是否与 Magento 版本相同?或者有些是 1.4,有些是 1.5 或 1.6...?...它们之间存在问题,您可以阅读每个版本的详细信息

标签: c# xml magento soap


【解决方案1】:

这很容易解决,但可能需要一些时间并且您需要更改一些代码。

问题:如果不低于 1.6.0,则不能使用 Magento WSDL 地址,因为它包含仅在 1.6 中修复的奇怪错误,请阅读 release notes of each version 了解更多信息

解决方案: 从您的项目中删除所有服务引用,并使用指向来自最新 Magento 的 wsdl 地址的 svcutil.exe 实用程序创建一个代理(即使您安装了最新版本仅用于此目的,迄今为止的最新稳定版本是 1.6.1)。

然后将生成的项目添加到您的 web/win 项目并使用其中的服务,并在 Client 对象中交换最终 url,例如:

MyNameSpace.MagentoSoapClient ws = new MyNameSpace.MagentoSoapClient();
ws.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://domain.com/api/v2_soap");

试试看。

【讨论】:

  • 嗨,再次感谢。原始程序本身就是这样编写的,因为我们必须在运行时切换 URL。不同之处在于代理是通过指向 Magento Go URL 创建的。但是在浏览了提琴手文件之后(正如你所建议的那样),我可以看到虽然对 Magento Go 商店的 salesOrderList() 调用返回了 salesOrderList[],但对 1.5.x 的相同调用返回了导致异常的 salesOrderListEntity[]。我现在也使用其他商店添加了一个服务引用,并根据需要在运行时在两个代理之间切换。有用。再次感谢您的帮助。
【解决方案2】:

好的,到此为止。原始程序中的服务引用是通过指向我们的 Magento Go 商店之一下的wsdl 地址创建的。该程序允许动态更改要使用的商店,因此在运行时将 URL 更改为附加了 /api/v2_soap/index 的相关商店 URL。

如问题中所述,销售订单导入适用于某些网站,而其他网站则失败。在 balexandre 提出了一些非常好的建议后,我们根据需要使用了 fiddler 和一些现场代码并解决了问题。

问题是:服务调用 salesOrderList()Magento Go 商店返回 salesOrderEntity[] ,为 1.5.x 商店返回 salesOrderListEntity[]。由于代理是使用 Magento Go 引用创建的,后一种类型对于程序来说是“意外的”,从而导致 Exception There is an error in XML document (2, 372) 与 @ 987654326@ 无法识别指定的类型:name='salesOrderListEntity',namespace='urn:Magento'。但是,我们从 Web 服务调用的所有其他方法都有效。但如果没有销售订单列表,它们本身并没有多大用处。


中间解决方法是:调整 wsdl 文件和 reference.cs 以声明“salesOrderListEntityand to create an overloaded version ofsalesOrderListEntity()”。但这种解决方法带来的问题比它解决的要多。


最终解决方案是:通过指向 Magento Go 和 Magento 1.5.x 创建两个不同的代理。在运行时切换到实际商店 URL 后,使用与当前商店相关的代理。相同的调用和流程,除了第一个代理知道它将收到salesOrderEntity[]salesOrderList() 调用,而第二个代理知道它将收到salesOrderListEntity[]

这没有问题,在我写这篇文章时,我们已经导入了几百个销售订单。它似乎也适用于 1.6.1 商店。但如果(上帝保佑)这方面有任何问题,我会告诉你的。

最后,我衷心感谢 balexandre 提供的所有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-16
    • 2013-10-12
    • 2014-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多