Question

I am using CookComputing XML-RPC connector to retrieve some data from a website. In the returned XML there is an object (address) that may or may not exist. If it exists then the object is deserialized into the class properly. If it does not exist then the returned object is of type array, not class. The deserialize throws an error regarding the type being wrong.

Can anyone assist in showing the best way to set up my code to catch both cases?

XML - Address Exists (Extract)

<member>
  <name>address</name>
  <value>
    <struct>
      <member>
        <name>parent_id</name>
        <value>
          <string>15088</string>
        </value>
      </member>
    </struct>
  </value>
</member>

XML - Address Non-Existant (Extract)

<member>
  <name>address</name>
    <value>
      <array>
        <data/>
      </array>
    </value>
</member>

C# constructor

private OrderAddress _shipping_address;
public OrderAddress shipping_address
{
    get { return _shipping_address; }
    set { _shipping_address = value; }
}
Was it helpful?

Solution

Just a note on the solution:

I had to load up the CookComputing XML-RPC project in Visual Studio and change the offending method to return an empty instance of the correct object.

XmlRpcDeserializer.cs

private object MapArray(IEnumerator<Node> iter, Type valType, 
  MappingStack mappingStack, MappingAction mappingAction,
  out Type mappedType)
{
  mappedType = null;
  // required type must be an array
  if (valType != null
    && !(valType.IsArray == true
        || valType == typeof(Array)
        || valType == typeof(object)))
  {
      if(valType.ToString().Contains("OrderAddress")){ //change this to your mismatch
          iter.MoveNext(); //moves increment on through the xml
          return Activator.CreateInstance(valType); //returns empty instance of correct object
      }

    throw new XmlRpcTypeMismatchException(mappingStack.MappingType
      + " contains array value where "
      + XmlRpcTypeInfo.GetXmlRpcTypeString(valType)
      + " expected " + StackDump(mappingStack));
  }

In theory you should be able to replace the throw and remove the if however I have kept it due to closed nature of objects I was working on and wanting to be in control to confirm each "error" was due to a null object return.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top