0

I am trying to implement a web service API from a Prestashop website in to my c# program using RestSharp. I'm having some trouble with the deserialization and it is causing null errors on my object it should be deserializing in to...

First, here is the XML that the GET function returned. I wrote the data directly from the IRestResponse that RestSharp gave me with this line:

System.IO.File.WriteAllText("response.txt", response.Content);

Here is the txt file that was saved from that:

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<customer>
<id><![CDATA[1]]></id>
<id_default_group xlink:href="http://removed.com/api/groups/3"><![CDATA[3]]></id_default_group>
<id_lang xlink:href="http://removed.com/api/languages/1"><![CDATA[1]]></id_lang>
<newsletter_date_add><![CDATA[2013-12-13 08:19:15]]></newsletter_date_add>
<ip_registration_newsletter></ip_registration_newsletter>
<last_passwd_gen><![CDATA[2014-06-20 16:56:30]]></last_passwd_gen>
<secure_key><![CDATA[6a9b9eab95448d74a026b869d8cd723e]]></secure_key>
<deleted><![CDATA[0]]></deleted>
<passwd><![CDATA[6028853eb1033578f7432015042fa486]]></passwd>
<lastname><![CDATA[DOE]]></lastname>
<firstname><![CDATA[John]]></firstname>
<email><![CDATA[pub@prestashop.com]]></email>
<id_gender><![CDATA[1]]></id_gender>
<birthday><![CDATA[1970-01-15]]></birthday>
<newsletter><![CDATA[1]]></newsletter>
<optin><![CDATA[1]]></optin>
<website></website>
<company></company>
<siret></siret>
<ape></ape>
<outstanding_allow_amount><![CDATA[0.000000]]></outstanding_allow_amount>
<show_public_prices><![CDATA[0]]></show_public_prices>
<id_risk><![CDATA[0]]></id_risk>
<max_payment_days><![CDATA[0]]></max_payment_days>
<active><![CDATA[1]]></active>
<note></note>
<is_guest><![CDATA[0]]></is_guest>
<id_shop><![CDATA[1]]></id_shop>
<id_shop_group><![CDATA[1]]></id_shop_group>
<date_add><![CDATA[2014-08-01 13:20:37]]></date_add>
<date_upd><![CDATA[2014-08-01 13:20:37]]></date_upd>
<associations>
<groups node_type="group">
<group xlink:href="http://removed.com/api/groups/3">
<id><![CDATA[3]]></id>
</group>
</groups>
</associations>
</customer>
</prestashop>

To try to serialize that with just a few of the elements (first name and last name) I tried to use this code:

public class prestashop
{
    public customer customer { get; set; }
}

public class customer
{
    public string firstname { get; set; }
    public string lastname { get; set; }
}

and then for the actual call itself:

// Attempt a request with XML Deserialization
request = new RestRequest { RequestFormat = DataFormat.Xml };
request.Resource = "/customers/1";
IRestResponse<prestashop> newResponse = client.Execute<prestashop>(request);
prestashop shopData = newResponse.Data;
Console.WriteLine("Data read: " + shopData.customer.firstname);

And that throws me an error on the final line of "Object reference not set to an instance of an object".

From that I'm assuming the deserialization is wrong somewhere, but I'm not sure where... the class I am attempting to create is so basic?

Something that I am wondering is the very first line of the XML file, could that be messing up the parsing somehow since it is actually including the XML line on top?

(Edit: I'm not sure why I got marked as duplicate for null objects... I already know why it's null and I stated this and provided code - I know it's null because RestSharp is not deserializing correctly in to my object. Knowing it is null does nothing, what I need to know is why it's not deserializing, because according to the rules provided by RestSharp I believe my prestashop object is created correctly? What, exactly, is the problem with deserialization?

The linked post does absolutely nothing to help me, as it does not explain why the deserialization of RestSharp is not working....)

  • *And that throws me an error on the final line of "Object reference not set to an instance of an object".* The first step is to find out what is null. – Ant P Aug 06 '14 at 21:50
  • The null is the information that should have been deserialized. If I do Console.WriteLine(newResponse.Content); - the full XML data is received. It's just the prestashop object that should be deserialized, contains no data. – user3916125 Aug 06 '14 at 21:53
  • Also, Console.WriteLine(newResponse.Data.customer.firstname); - That gives the same error. So it's the deserialized data that is not getting populated it seems? – user3916125 Aug 06 '14 at 22:01
  • Is `shopData` null, is `shopData.customer` null, or is `shopData.customer.firstname` null? – dbc Aug 06 '14 at 22:52
  • So I tried a simple if(shopData == null) - etc, for the 3 variables you just listed, with a console.writeline of the result. In console, shopData was NOT null. shopData.customer WAS null, and "if (shopData.customer.firstname == null)" gave the same error as earlier.... Isn't that abnormal? Shouldn't it at least be able to check for null w/o erroring? – user3916125 Aug 06 '14 at 23:00
  • @user3916125 - You asked: "Shouldn't it at least be able to check for null w/o erroring?" You need to show us your code. But probably you forgot to do "else" after each null check. – dbc Aug 06 '14 at 23:43
  • You aren't checking if customer is null, you're accessing customer and checking if firstname is null. You can't do that if customer is null. – Ant P Aug 07 '14 at 07:15
  • Does anyone see any errors with the object I created as to why the serialization should not be working? The only child object of Prestashop is "customer"... I'm not sure how I could possibly change the object format if it only has one child, what changes could I do? – user3916125 Aug 07 '14 at 16:13

1 Answers1

0

So I discovered the answer.

It turns out in the above code, the "prestashop" object was being deserialized as customer. The prestashop XML was ignored by the deserializer. All of the children of customer were able to be accessed through the prestashop class.