C# 导致空对象的RestSharp反序列化

C# 导致空对象的RestSharp反序列化,c#,xml,api,restsharp,C#,Xml,Api,Restsharp,我正在尝试使用RestSharp将一个web服务API从Prestashop网站实现到我的c#程序中。我在反序列化方面遇到了一些问题,它在我的对象上导致了空错误,它应该反序列化到 首先,下面是GET函数返回的XML。我直接从RestSharp提供给我的iRestreponse中写入了以下数据: System.IO.File.WriteAllText("response.txt", response.Content); 以下是从中保存的txt文件: <?xml version="1.0"

我正在尝试使用RestSharp将一个web服务API从Prestashop网站实现到我的c#程序中。我在反序列化方面遇到了一些问题,它在我的对象上导致了空错误,它应该反序列化到

首先,下面是GET函数返回的XML。我直接从RestSharp提供给我的iRestreponse中写入了以下数据:

System.IO.File.WriteAllText("response.txt", response.Content);
以下是从中保存的txt文件:

<?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>
然后对于实际调用本身:

// 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);
//尝试XML反序列化请求
request=newrestrequest{RequestFormat=DataFormat.Xml};
request.Resource=“/customers/1”;
IRestResponse newResponse=client.Execute(请求);
prestashop shopData=newResponse.Data;
Console.WriteLine(“数据读取:”+shopData.customer.firstname);
这让我在“对象引用未设置为对象实例”的最后一行出现了一个错误

因此我假设反序列化在某个地方是错误的,但我不确定在哪里。。。我试图创建的类是如此基本

我想知道的是XML文件的第一行,它是否会在某种程度上扰乱解析,因为它实际上在顶部包含XML行

(编辑:我不知道为什么我被标记为空对象的副本…我已经知道为什么它是空的,我说明了这一点并提供了代码-我知道它是空的,因为RestSharp没有在我的对象中正确地反序列化。知道它是空的没有任何作用,我需要知道的是为什么它没有反序列化,因为根据rul我相信我的prestashop对象是正确创建的?反序列化的问题到底是什么


链接帖子对我毫无帮助,因为它没有解释RestSharp的反序列化为什么不起作用……

所以我找到了答案


在上面的代码中,“prestashop”对象被反序列化为customer。反序列化程序忽略了prestashop XML。customer的所有子对象都可以通过prestashop类进行访问。

这在“对象引用未设置为对象实例”的最后一行引发了一个错误。第一步是找出什么是null。null是应该反序列化的信息。如果我执行Console.WriteLine(newResponse.Content);-将接收完整的XML数据。应该反序列化的只是prestashop对象,不包含任何数据。此外,Console.WriteLine(newResponse.data.customer.firstname)-这给出了相同的错误。所以反序列化的数据似乎没有被填充?is
shopData
null,is
shopData.customer
null,或者is
shopData.customer.firstname
null?所以我尝试了一个简单的if(shopData==null)-etc,对于刚才列出的3个变量,结果是console.writeline。在console中,shopData不是null。shopData.customer是null,并且“if(shopData.customer.firstname==null)”给出了与前面相同的错误…这不是异常吗?它至少应该能够检查null w/o错误吗?
// 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);