使用 RestSharp 反序列化 XML 响应 [英] Deserializing XML Response using RestSharp
问题描述
我已经阅读了这里已经提出的关于这个主题的各种问题,但我仍然没有接近解决我的问题.
I have read through the various questions already posed here regarding this subject and i'm still no closer to solving my problem.
我正在尝试反序列化这个 xml 响应:
I am trying to Deserialize this xml response:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<SubmissionResult>
<Result>ACCEPTED</Result>
<SubmissionID>
<RefNo>77587-1425386500</RefNo>
<Submitted>9</Submitted>
<ValidNo>7</ValidNo>
<InvalidNo>2</InvalidNo>
<InvalidNo_1>08452782055</InvalidNo_1>
<InvalidNo_2>01234567890</InvalidNo_1>
<TextvID>77587-1425386500</TextvID>
</SubmissionID>
<Credits>999999</Credits>
</SubmissionResult>
使用这些类:
[XmlRoot ("SubmissionResult")]
public class SubmissionResult
{
[XmlElement ("Result")]
public string Result { get; set; }
public SubmissionID SubmissionID { get; set; }
[XmlElement("Credits")]
public int Credits { get; set; }
}
public class SubmissionID
{
[XmlElement("RefNo")]
public int RefNo { get; set; }
[XmlElement("Submitted")]
public int Submitted { get; set; }
[XmlElement("ValidNo")]
public int ValidNo { get; set; }
[XmlElement("TextVID")]
public string TextVID { get; set; }
}
但是我只返回 null 和 0 值,我认为这是默认值.
However I am only ever returning null and 0 values, which I assume are the default.
这是获取结果并希望反序列化的代码:
Here is the code to get the results and hopefully deserialize:
try
{
var request = new RestRequest();
request.RequestFormat = DataFormat.Xml;
request.Resource = APIURL;
request.RootElement = "SubmissionResult";
SubmissionResult r = Execute<SubmissionResult>(request);
}
public static T Execute<T>(RestRequest request) where T : new()
{
var client = new RestClient();
client.BaseUrl = new Uri("https://www.textvertising.co.uk/_admin/api", UriKind.Absolute);
var response = client.Execute<T>(request);
return response.Data;
}
非常感谢您提供的任何帮助.
Many thanks in advance for any help you can provide.
CS
推荐答案
我做了一些改动,可以让它工作:
I made a few changes and could make it work:
问题中可能有错别字,但我必须更改的是已发布的 XML 无效:
Probably a typo in the question but something I had to change is that posted XML is invalid:
<InvalidNo_2>01234567890</InvalidNo_1>
我知道您想使用 .NET XMLSerializer,因为您使用了 XmlRoot 和 XmlElement 注释,因此您必须像这样覆盖 RestSharp 附带的默认注释:
I understand you want use .NET XMLSerializer as you used XmlRoot and XmlElement annotations so you have to override the default one that comes with RestSharp like so:
request.XmlSerializer = new RestSharp.Serializers.DotNetXmlSerializer();
此外,我不得不删除与 .NET 序列化程序不兼容的 RootElement 设置,因此删除了以下行:
Also I had to delete the RootElement setting which didn't play nicely with .NET serializer so deleted the following line:
request.RootElement = "SubmissionResult";
所以我的版本变成了:
var request = new RestRequest();
request.XmlSerializer = new RestSharp.Serializers.DotNetXmlSerializer();
request.RequestFormat = DataFormat.Xml;
// request.Resource = APIURL;
SubmissionResult r = Execute<SubmissionResult>(request);
最后我注意到 RefNo 在类中应该是整数,但返回的值不是 (77587-1425386500) 所以我把它变成了一个字符串:
Finally I noticed RefNo is meant to be integer in the class but the returned value is not (77587-1425386500) so I made it a string:
[XmlElement("RefNo")]
public string RefNo { get; set; }
我在 mocky.io 上创建了一个模拟响应,并对此进行了测试,它似乎工作正常:
I created a mock response at mocky.io at tested with that and it seems to be working fine:
public static T Execute<T>(RestRequest request) where T : new()
{
var client = new RestClient();
client.BaseUrl = new Uri("http://www.mocky.io/v2/56cd88660f00009800d61ff8", UriKind.Absolute);
var response = client.Execute<T>(request);
return response.Data;
}
这篇关于使用 RestSharp 反序列化 XML 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!