XML反序列化不会初始化数组元素

时间:2014-10-17 13:50:03

标签: c# xml serialization deserialization

我正在尝试反序列化以下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>

我正在使用的代码(T的类型为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);
}            

课程:

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

反序列化不会抛出任何异常,但Clients数组为null。我有什么想法可以解决它吗?我现在已经尝试了几个小时,我无法理解。

1 个答案:

答案 0 :(得分:1)

您的AffinitasClientRoot课程应如下所示:

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

即。插入Namespace=""声明以从'named namespace''切换回'到'无名空间'。