简体   繁体   中英

RestSharp deserialization causing null object

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.... )

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM