我需要下一个xml数据:
<trackingrequest>
<user>USERNAME</user>
<password>PASSWORD</password>
<trackingnumbers>
<trackingnumber>12345678901</trackingnumber>
</trackingnumbers>
</trackingrequest>
为此,我创建了下一个模型:
[SerializeAs(Name = "trackingrequest")]
public class DpdTrackingRequest
{
[SerializeAs(Name = "user")]
public string User { get; set; }
[SerializeAs(Name = "password")]
public string Password { get; set; }
[SerializeAs(Name = "trackingnumbers")]
[XmlArrayItem("trackingnumber")]
public IList<DpdTrackingNumber> TrackingNumbers { get; set; }
}
public class DpdTrackingNumber
{
public string Value { get; set; }
}
但是当我调用AddXmlBody
方法时,我会看到下一个结果:
<trackingrequest>
<user>USERNAME</user>
<password>PASSWORD</password>
<trackingnumbers>
<DpdTrackingNumber>
<Value>12345678901</Value>
</DpdTrackingNumber>
</trackingnumbers>
</trackingrequest>
如何在<DpdTrackingNumber><Value>12345678901</Value></DpdTrackingNumber>
中合并<trackingnumber>12345678901</trackingnumber>
?
答案 0 :(得分:2)
为了达到您的要求,我认为您必须将System.Xml.Serialization.XmlTextAttribute
设置为DpdTrackingNumber.Value
属性。
但是,RestRequest
的{{1}}默认为XmlSerializer
,无法读取RestSharp.Serializers.XmlSerializer
属性。
因此,在调用System.Xml.Serialization
方法之前,您必须将XmlSerializer
重置为RestSharp.Serializers.DotNetXmlSerializer
。
AddXmlBody
此外,您可能需要将dpdTrackingRequest.TrackingNumbers = new List<DpdTrackingNumber>();
dpdTrackingRequest.TrackingNumbers.Add(new DpdTrackingNumber { Value = "12345678901" });
RestRequest restRequest = new RestRequest();
// reset XmlSerializer
restRequest.XmlSerializer = new RestSharp.Serializers.DotNetXmlSerializer();
var result = restRequest.AddXmlBody(dpdTrackingRequest);
的类型从TrackingNumbers
更改为IList
,因为List
无法对接口进行序列化,因此DotNetXmlSerializer
类如下:
DpdTrackingRequest