ASP.NET Core – Deserialize Cities API XML response not working

When calling cities API, it returns this response in XML format. I want to deserialize this XML into an object. Deserialization grabs objects till diffgram inside diffgram we have the NewDataSet property which returns null and I need to get Cities.

API response:

<?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>

Code details:

 [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)!;
      }
  }

Calling API and deserializing XML response:

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 };
    }
}

Output:

I have created model classes and decorated these models with XmlElement annotations to deserialize the XML response to get city names located inside the RetailCities list. Still, unfortunately, I’m unable to reach the RetailCities as NewDataSet returns null.

From your XML <NewDataSet xmlns="">, you should add the Namespace = "" in the XmlElement attribute for the NewDataSet property in the Diffgram class.

[XmlRoot(ElementName = "diffgram", Namespace = "urn:schemas-microsoft-com:xml-diffgram-v1")]
public class Diffgram
{
    [XmlElement(ElementName = "NewDataSet", Namespace = "")]
    public NewDataSet NewDataSet { get; set; }
}

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật