Search code examples
c#web-servicessoapc#-3.0variable-initialization

sending a SOAP request with c#


I'm trying to send a SOAP request to a 3rd party web service. I've successfully send and received data from other interfaces in the same service, but I'm having problems with this particular one:

<SP_GoodsMovement xmlns="http://services.hnseu.com">
  <GoodsMoved xmlns="http://tempuri.org/SP_GoodsMoved.xsd">
    <SerialNumberedGoodsMovements>
      <SerialNumbered>
        <PartNumber>string</PartNumber>
        <SerialNumber>string</SerialNumber>
        <MovementType>string</MovementType>
        <FromLocation>string</FromLocation>
        <FromLocationCategory>string</FromLocationCategory>
        <ToLocation>string</ToLocation>
        <ToLocationCategory>string</ToLocationCategory>
        <AssetMovementTimestamp>dateTime</AssetMovementTimestamp>
        <GoodsInReference>string</GoodsInReference>
      </SerialNumbered>
      <SerialNumbered>
        <PartNumber>string</PartNumber>
        <SerialNumber>string</SerialNumber>
        <MovementType>string</MovementType>
        <FromLocation>string</FromLocation>
        <FromLocationCategory>string</FromLocationCategory>
        <ToLocation>string</ToLocation>
        <ToLocationCategory>string</ToLocationCategory>
        <AssetMovementTimestamp>dateTime</AssetMovementTimestamp>
        <GoodsInReference>string</GoodsInReference>
      </SerialNumbered>
    </SerialNumberedGoodsMovements>
    <NonSerialNumberedGoodsMovements>
      <NonSerialNumbered>
        <PartNumber>string</PartNumber>
        <Quantity>unsignedInt</Quantity>
        <MovementType>string</MovementType>
        <FromLocation>string</FromLocation>
        <FromLocationCategory>string</FromLocationCategory>
        <ToLocation>string</ToLocation>
        <ToLocationCategory>string</ToLocationCategory>
        <Used>boolean</Used>
        <AssetMovementTimestamp>dateTime</AssetMovementTimestamp>
        <GoodsInReference>string</GoodsInReference>
      </NonSerialNumbered>
      <NonSerialNumbered>
        <PartNumber>string</PartNumber>
        <Quantity>unsignedInt</Quantity>
        <MovementType>string</MovementType>
        <FromLocation>string</FromLocation>
        <FromLocationCategory>string</FromLocationCategory>
        <ToLocation>string</ToLocation>
        <ToLocationCategory>string</ToLocationCategory>
        <Used>boolean</Used>
        <AssetMovementTimestamp>dateTime</AssetMovementTimestamp>
        <GoodsInReference>string</GoodsInReference>
      </NonSerialNumbered>
    </NonSerialNumberedGoodsMovements>
  </GoodsMoved>
</SP_GoodsMovement>

so my code is as follows (i can expand this if necesssary):

...
if (requestType == "SP_GoodsMovement")
{
  GoodsMoved SOAP_GoodsMoved = new GoodsMoved();
  SOAP_GoodsMoved.SerialNumberedGoodsMovements[0].PartNumber = partNumber[0].InnerXml;
...
  string SOAPMessage;
  SOAPMessage = request.SP_GoodsMovement(header, SOAP_GoodsMoved).Message;
}

When I run this code I get an 'Object reference not set to an instance of an object' error.

I think i'm not referencing the PartNumber parameter properly, but i've tried a few things without success.

Any ideas?


Solution

  •    SOAP_GoodsMoved.SerialNumberedGoodsMovements[0]
    

    doesn't appear to be initialised.

    maybe try

       GoodsMoved SOAP_GoodsMoved = new GoodsMoved();
       SOAP_GoodsMoved.SerialNumberedGoodsMovements = new WhateverObject[1];
       SOAP_GoodsMoved.SerialNumberedGoodsMovements[0] = new WhateverObject();
       SOAP_GoodsMoved.SerialNumberedGoodsMovements[0].PartNumber = partNumber[0].InnerXml;
    

    or you could right an overload for your GoodsMoved() ctor that ensures that the SerialNumberedGoodsMovements array gets initialized with a certain size.