在c#中读取SOAP数组

时间:2010-09-22 19:22:36

标签: c# soap

我正在从第三方网站上使用网络服务。我在wsdl文件中有类型和方法,名为GetItemAvailable。当我使用并执行服务时,它返回一个SOAP数组(在wsdl中定义为ItemsArray)。 问题是,我不确定我需要使用什么类型来获取信息。这是我的代码:

GetItemAvailable.GetItemAvailable gia = new GetItemAvailable.GetItemAvailable();
ItemsArray ia = new ItemsArray();
ia = (gia.CallGetItemAvailable("1088507721", "9780842387491"));
string sttest = ia.id[1].ToString();

错误始终表示ia.id为空

我在做错了什么?我对SOAP不太熟悉。 这是wsdl:

<wsdl:definitions>
  <wsdl:types>
    <xsd:complexType name="Items">
        <xsd:all>
          <xsd:element name="item_id" type="xsd:string" />
          <xsd:element name="tn_onhand" type="xsd:int" />
          <xsd:element name="tn_onorder" type="xsd:string" />
          <xsd:element name="tn_onorder_eta" type="xsd:string" />
          <xsd:element name="nv_onhand" type="xsd:int" />
          <xsd:element name="nv_onorder" type="xsd:string" />
          <xsd:element name="nv_onorder_eta" type="xsd:string" />
          <xsd:element name="error" type="xsd:string" />
        </xsd:all>
      </xsd:complexType>
      <xsd:complexType name="ItemsArray">
        <xsd:complexContent mixed="false">
          <xsd:restriction base="SOAP-ENC:Array">
            <xsd:attribute wsdl:arrayType="tns:Items[]" ref="SOAP-ENC:arrayType" />
          </xsd:restriction>
        </xsd:complexContent>
      </xsd:complexType>
      <xsd:complexType name="GetItemAvailableRequestType">
        <xsd:all>
          <xsd:element name="api_key" type="xsd:string" />
          <xsd:element name="Items" type="xsd:string" />
        </xsd:all>
      </xsd:complexType>
      <xsd:complexType name="GetItemAvailableResponseType">
        <xsd:all>
          <xsd:element name="GetItemAvailableResult" type="tns:ItemsArray" />
        </xsd:all>
      </xsd:complexType>
      <xsd:element name="GetItemAvailable" type="tns:GetItemAvailableRequestType" />
      <xsd:element name="GetItemAvailableResponse" type="tns:GetItemAvailableResponseType" />
    </xsd:schema>
  </wsdl:types>
  <wsdl:message name="GetItemAvailableRequest">
    <wsdl:part name="parameters" element="tns:GetItemAvailable" />
  </wsdl:message>
  <wsdl:message name="GetItemAvailableResponse">
    <wsdl:part name="parameters" element="tns:GetItemAvailableResponse" />
  </wsdl:message>
  <wsdl:portType name="GetItemAvailablePortType">
    <wsdl:operation name="GetItemAvailable">
      <documentation>Returns quantity available and on order for list of products seperated by comma (example: 9780877840756,9780446691093)</documentation>
      <wsdl:input message="tns:GetItemAvailableRequest" />
      <wsdl:output message="tns:GetItemAvailableResponse" />
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="GetItemAvailableBinding" type="tns:GetItemAvailablePortType">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="GetItemAvailable">
      <soap:operation soapAction="http://www.stl-distribution.com/GetItemAvailable" style="document" />
      <wsdl:input>
        <soap:body use="literal" namespace="http://www.stl-distribution.com" />
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" namespace="http://www.stl-distribution.com" />
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="GetItemAvailable">
    <wsdl:port name="GetItemAvailablePort" binding="tns:GetItemAvailableBinding">
      <soap:address location="http://www.stl-distribution.com/webservices/soap/GetItemAvailable.php" />
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

4 个答案:

答案 0 :(得分:4)

不应该是

//string sttest = ia.id[1].ToString();
string sttest = ia[0].item_id;

答案 1 :(得分:1)

Henk说的有意义。

此外,这是为您编译的。基于下面的wsdl:

<xsd:complexType name="Items"> 
        <xsd:all> 
          <xsd:element name="item_id" type="xsd:string" /> 
          <xsd:element name="tn_onhand" type="xsd:int" /> 
          <xsd:element name="tn_onorder" type="xsd:string" /> 
          <xsd:element name="tn_onorder_eta" type="xsd:string" /> 
          <xsd:element name="nv_onhand" type="xsd:int" /> 
          <xsd:element name="nv_onorder" type="xsd:string" /> 
          <xsd:element name="nv_onorder_eta" type="xsd:string" /> 
          <xsd:element name="error" type="xsd:string" /> 
        </xsd:all> 
</xsd:complexType>

我希望ia [x] .item_id - 这个值应该是字符串。

所以也许你的代码应该是这样的:

GetItemAvailable.GetItemAvailable gia = new GetItemAvailable.GetItemAvailable();      
ItemsArray ia = new ItemsArray();      
ia = (gia.CallGetItemAvailable("1088507721", "9780842387491"));      
string sttest = ia[0].item_id;

答案 2 :(得分:1)

我通过编写一个新类来解决WSDL文件来解决问题。 猜猜我应该首先做到这一点。 感谢所有帮助人员!!!

 public class STLItemAvailable
    {
        private string _itemNumber;
        public string ItemNumber
        {
            get
            {
                return _itemNumber;
            }
        }

        private int _tnOnHand;
        public int TNOnHand
        {
            get
            {
                return _tnOnHand;
            }
        }

        private int _tnOnOrder;
        public int TNOnOrder
        {
            get
            {
                return _tnOnOrder;
            }
        }

        private DateTime _tnOrderETA;
        public DateTime TNOrderETA
        {
            get
            {
                return _tnOrderETA;
            }
        }

        private int _nvOnHand;
        public int NVOnHand
        {
            get
            {
                return _nvOnHand;
            }
        }

        private int _nvOnOrder;
        public int NVOnOrder
        {
            get
            {
                return _nvOnOrder;
            }
        }

        private DateTime _nvOrderETA;
        public DateTime NVOrderETA
        {
            get
            {
                return _nvOrderETA;
            }
        }

        public STLItemAvailable()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public STLItemAvailable(string APIKey, string ItemNumber)
        {
            GetItemAvailable gia = new GetItemAvailable();
            ItemsArray ia = gia.CallGetItemAvailable(APIKey, ItemNumber);

            XmlElement[] XEArray = ia.Any;
            XmlNode n = XEArray[0];

            int testn = 0;
            DateTime testd;
            foreach (XmlNode xn in n.ChildNodes)
            {
                switch (xn.Name)
                {
                    case "item_id":
                        this._itemNumber = xn.InnerText;
                        break;
                    case "tn_onhand":
                        int.TryParse(xn.InnerText, out testn);
                        this._tnOnHand = testn;
                        break;
                    case "tn_onorder":
                        int.TryParse(xn.InnerText, out testn);
                        this._tnOnOrder = testn;
                        break;
                    case "tn_onorder_eta":
                        DateTime.TryParse(xn.InnerText, out testd);
                        this._tnOrderETA = testd;
                        break;
                    case "nv_onhand":
                        int.TryParse(xn.InnerText, out testn);
                        this._nvOnHand = testn;
                        break;
                    case "nv_onorder":
                        int.TryParse(xn.InnerText, out testn);
                        this._nvOnOrder = testn;
                        break;
                    case "nv_onorder_eta":
                        DateTime.TryParse(xn.InnerText, out testd);
                        this._nvOrderETA = testd;
                        break;

答案 3 :(得分:0)

这可能有所帮助,但值得一试。再看一下wsdl:

<xsd:complexType name="GetItemAvailableResponseType">
  <xsd:all> 
    <xsd:element name="GetItemAvailableResult" type="tns:ItemsArray" /> 
  </xsd:all> 
</xsd:complexType>

似乎Response有一个对象GetItemsAvailableResult实际上是f类型ItemsArray。

你试过ia.GetItemsAvailableResult [0] .item_id了吗?

希望这会有所帮助。顺便说一句,我添加了另一个答案,因为在评论中添加代码片段会太麻烦。

干杯,瓦格纳。