调用城市 API 时,它以 XML 格式返回此响应。我想将此 XML 反序列化为一个对象。反序列化抓取对象,直到 diffgram 内的 diffgram 我们有
NewDataSet
属性,该属性返回 null,我需要获取 Cities。
API响应:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<getRTLCitiesResponse xmlns="http://track.smsaexpress.com/secom/">
<getRTLCitiesResult>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="RetailCities">
<xs:complexType>
<xs:sequence>
<xs:element name="routCode" type="xs:string" minOccurs="0" />
<xs:element name="rCity" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<NewDataSet xmlns="">
<RetailCities diffgr:id="RetailCities1" msdata:rowOrder="0">
<routCode>ABT</routCode>
<rCity>Aqiq</rCity>
</RetailCities>
<RetailCities diffgr:id="RetailCities2" msdata:rowOrder="1">
<routCode>ABT</routCode>
<rCity>Atawlah</rCity>
</RetailCities>
<RetailCities diffgr:id="RetailCities3" msdata:rowOrder="2">
<routCode>ABT</routCode>
<rCity>Baha</rCity>
</RetailCities>
</NewDataSet>
</diffgr:diffgram>
</getRTLCitiesResult>
</getRTLCitiesResponse>
</soap:Body>
</soap:Envelope>
代码详情:
[XmlRoot(ElementName = "RetailCities")]
public class RetailCity
{
[XmlElement(ElementName = "routCode")]
public string RoutCode { get; set; }
[XmlElement(ElementName = "rCity")]
public string RCity { get; set; }
}
[XmlRoot(ElementName = "NewDataSet")]
public class NewDataSet
{
[XmlElement(ElementName = "RetailCities")]
public List<RetailCity> RetailCities { get; set; } = new List<RetailCity>();
}
[XmlRoot(ElementName = "diffgram", Namespace = "urn:schemas-microsoft-com:xml-diffgram-v1")]
public class Diffgram
{
[XmlElement(ElementName = "NewDataSet")]
public NewDataSet NewDataSet { get; set; }
}
[XmlRoot(ElementName = "getRTLCitiesResult", Namespace = "http://track.smsaexpress.com/secom/")]
public class GetRTLCitiesResult
{
[XmlElement(ElementName = "diffgram", Namespace = "urn:schemas-microsoft-com:xml-diffgram-v1")]
public Diffgram Diffgram { get; set; }
}
[XmlRoot(ElementName = "getRTLCitiesResponse", Namespace = "http://track.smsaexpress.com/secom/")]
public class GetRTLCitiesResponse
{
[XmlElement(ElementName = "getRTLCitiesResult")]
public GetRTLCitiesResult GetRTLCitiesResult { get; set; }
}
[XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class SoapBody
{
[XmlElement(ElementName = "getRTLCitiesResponse", Namespace = "http://track.smsaexpress.com/secom/")]
public GetRTLCitiesResponse GetRTLCitiesResponse { get; set; }
}
[XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class SoapEnvelope
{
[XmlElement(ElementName = "Body")]
public SoapBody Body { get; set; }
[XmlNamespaceDeclarations]
public XmlSerializerNamespaces Xmlns { get; set; } = new XmlSerializerNamespaces();
public SoapEnvelope()
{
Xmlns.Add("soap", "http://schemas.xmlsoap.org/soap/envelope/");
Xmlns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
Xmlns.Add("xsd", "http://www.w3.org/2001/XMLSchema");
}
}
private static T DeserializeXml<T>(string xml)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (StringReader reader = new StringReader(xml))
{
return (T)serializer.Deserialize(reader)!;
}
}
调用API并反序列化XML响应:
using (var client = new HttpClient())
{
var content = new StringContent(AramexCitiesSoapEnvelope(), Encoding.UTF8, "text/xml");
try
{
var response = await client.PostAsync(endPoint, content);
if (response.IsSuccessStatusCode)
{
// Read and return the response content
var resultContent = await response.Content.ReadAsStringAsync();
var envelope = DeserializeXml<SoapEnvelope>(resultContent);
return envelope;
}
else
{
// Return the failed response content
var errorContent = await response.Content.ReadAsStringAsync();
return new SoapEnvelope() { ErrorMessage = errorContent };
}
}
catch (HttpRequestException e)
{
return new SoapEnvelope() { ErrorMessage = e.Message };
}
}
输出:
我创建了模型类,并使用
XmlElement
注释装饰这些模型,以反序列化 XML 响应以获取位于 RetailCities
列表内的城市名称。不过,不幸的是,我无法到达 RetailCities
,因为 NewDataSet
返回 null。
在 XML
<NewDataSet xmlns="">
中,您应该在 Namespace = ""
类中 XmlElement
属性的 NewDataSet
属性中添加 Diffgram
。
[XmlRoot(ElementName = "diffgram", Namespace = "urn:schemas-microsoft-com:xml-diffgram-v1")]
public class Diffgram
{
[XmlElement(ElementName = "NewDataSet", Namespace = "")]
public NewDataSet NewDataSet { get; set; }
}