0

I am trying to deserialize the following XML:

<ns0:L2CCustomer xmlns:ns0="http://CustomerSyncFromMDM_L2C.L2CCustomer_XML">
  <customerList>
    <customer>
      <DNV_Status>Active</DNV_Status>
      <MDM_ID>1270004</MDM_ID>
      <fullName>JONBER030</fullName>
      <AFFRowID>1-GT6E0X</AFFRowID>
      <addressList>
        <address>
          <addrType>Office</addrType>
          <addrStatus>Active</addrStatus>
          <MDM_ID>2090001</MDM_ID>
          <addrLine1>Tårnveien 14</addrLine1>
          <addrLine2>Buildling 2</addrLine2>
          <city>Ås</city>
          <postalCode>1430</postalCode>
          <country>Norway</country>
          <validCountryISO2>NO</validCountryISO2>
          <AFFRowID>1-GT6E13</AFFRowID>
        </address>
      </addressList>
    </customer>
   </customerList>
</ns0:L2CCustomer>

The code that I am using (T is of type AffinitasClientRoot):

System.IO.StreamReader r = new System.IO.StreamReader(m.BodyStream);
string cze = r.ReadToEnd();
var serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
T result;
using (System.IO.TextReader reader = new System.IO.StringReader(cze))
{
    result = (T)serializer.Deserialize(reader);
}            

The classes:

[Serializable]
[XmlRoot(ElementName = "L2CCustomer", Namespace = "http://CustomerSyncFromMDM_L2C.L2CCustomer_XML")]
[XmlType("L2CCustomer")]
public class AffinitasClientRoot
{
    [XmlArray("customerList")]
    [XmlArrayItem("customer")]
    public AffinitasClient[] Clients { get; set; }
}

[Serializable]
[XmlRoot(ElementName = "customer")]
[XmlType("customer")]
public class AffinitasClient
{
    [XmlElement(ElementName = "AFFRowID")]
    public string RowId { get; set; }
    [XmlElement(ElementName = "MDM_ID")]
    public string MdmId { get; set; }
    [XmlElement(ElementName = "fullName")]
    public string Name { get; set; }
    [XmlElement(ElementName = "DNV_Status")]
    public string Status { get; set; }
    public bool Active
    {
        get
        {
            return Status == "Active";
        }
    }
    [XmlArray("addressList")]
    [XmlArrayItem("address")]
    public AffinitasClientAddress[] Addresses { get; set; }
    [XmlElement(ElementName = "lastUpdatedByTimestamp")]
    public DateTime LastUpdateDate { get; set; }
    //[XmlElement(ElementName = "Updated")]
    //public DateTime LastUpdateDate { get; set; }
}

[Serializable]
[XmlRoot(ElementName = "address")]
[XmlType("address")]
public class AffinitasClientAddress
{
    [XmlElement(ElementName = "AFFRowID")]
    public string RowId { get; set; }
    [XmlElement(ElementName = "MDM_ID")]
    public string MdmId { get; set; }
    [XmlElement(ElementName = "addrType")]
    public string AddressType { get; set; }
    [XmlElement(ElementName = "addrStatus")]
    public string Status { get; set; }
    public bool Active
    {
        get
        {
            return Status == "Active";
        }
    }
    [XmlElement(ElementName = "addrLine1")]
    public string Address1 { get; set; }
    [XmlElement(ElementName = "addrLine2")]
    public string Address2 { get; set; }
    [XmlElement(ElementName = "city")]
    public string City { get; set; }
    [XmlElement(ElementName = "postalCode")]
    public string PostalCode { get; set; }
    [XmlElement(ElementName = "country")]
    public string Country { get; set; }
    [XmlElement(ElementName = "validCountryISO2")]
    public string CountryCode { get; set; }
    [XmlElement(ElementName = "lastUpdatedByTimestamp")]
    public DateTime LastUpdateDate { get; set; }
}

The deserialization does not throw any exception, but the Clients array is null. Any ideas how I can solve it? I've been trying to do it for a few hours now and I cannot figure it out.

1 Answer 1

1

Your AffinitasClientRoot class should look like that:

public class AffinitasClientRoot
{
    [XmlArray("customerList", Namespace="")] // here!!!!
    [XmlArrayItem("customer")]
    public AffinitasClient[] Clients { get; set; }
}

I.e. insert the Namespace="" declaration to 'switch back' from 'named namespace' to 'nameless namespace'.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.