2016-06-07 50 views
2

我在将一些XML反序列化到对象列表时遇到了一些问题,在调试过程中检查DateAndTimeSlot时总是获得计数0并在该“原始数据”内。将XML反序列化到嵌套对象列表返回计数0

不幸的是我不能改变这些元素的名称。

但是,当检查XML时,我会返回XML中的DateAndTimeslot对象。

与其他对象列表我似乎很好,没有包含名称空间。

我错过了什么?

C#代码:

[XmlRoot("AppointmentAvailabilityStatusResponse")] 
    public class CheckAppointmentAvailabilityContainer 
    { 
     [XmlElement("AppointmentAvailabilityStatusResult")] 
     public AppointmentAvailabilityStatus appointmentAvailabilityStatus { get; set; } 
    } 
    [XmlRoot("AppointmentAvailabilityStatusResult", Namespace = "Appointments")] 
    public class AppointmentAvailabilityStatus 
    { 
     [XmlArray("DateAndTimeSlot", Namespace = "Appointments")] 
     [XmlArrayItem(typeof(DateAndTimeslot))] 
     public DateAndTimeSlots DateAndTimeSlot { get; set; } 
     [XmlElement("RequestedStatus")] 
     public int RequestedStatus { get; set; } 
    } 
    [XmlRoot(ElementName = "DateAndTimeSlot")] 
    [XmlType("a")] 
    public class DateAndTimeSlots : List<DateAndTimeslot> { } 

    [XmlRoot(ElementName = "DateAndTimeslot", Namespace = "Appointments.TO")] 
    [XmlType("b")] // if included this renames the node to "b" for some reason 
    public class DateAndTimeslot 
    { 
     [XmlElement("Date")] 
     public string Date { get; set; } 
     [XmlElement("TimeSlot")] 
     public string TimeSlot { get; set; } 
    } 

缩短返回的XML,我想完全deserialise。

<AppointmentAvailabilityStatusResponse> 
    <AppointmentAvailabilityStatusResult xmlns:a="Appointments" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
    <a:DateAndTimeSlot xmlns:b="Appointments.TO"> 
     <b:DateAndTimeslot> 
     <b:Date>14/07/2016</b:Date> 
     <b:TimeSlot>AM</b:TimeSlot> 
     </b:DateAndTimeslot> 
     <b:DateAndTimeslot> 
     <b:Date>14/07/2016</b:Date> 
     <b:TimeSlot>PM</b:TimeSlot> 
     </b:DateAndTimeslot> 
    </a:DateAndTimeSlot> 
    <a:RequestStatus>0</a:RequestStatus> 
    </AppointmentAvailabilityStatusResult> 
</AppointmentAvailabilityStatusResponse> 

XML如果我连载虚拟对象 - 其中一些我想要的差异整顿,不知道该命名空间是必要的deserialisation虽然

<AppointmentAvailabilityStatusResponse> 
<AppointmentAvailabilityStatusResult> 
<DateAndTimeSlot xmlns=\"Appointments\"> 
<DateAndTimeslot> 
<Date xmlns=\"Appointments.TO\">today</Date> 
<TimeSlot xmlns=\"Appointments.TO\">now</TimeSlot> 
</DateAndTimeslot> 
</DateAndTimeSlot> 
<RequestedStatus xmlns=\"Appointments\">0</RequestedStatus> 
</AppointmentAvailabilityStatusResult> 
</AppointmentAvailabilityStatusResponse> 

解串器

public static T DeserializeThis<T>(string cleanXml) 
     { 
      //string cleanXml = RemoveBom(dirtyXml); 
      bool check = cleanXml.TrimStart().StartsWith("<"); 
      if (!string.IsNullOrEmpty(cleanXml) && cleanXml.TrimStart().StartsWith("<")) 
      { 
       try 
       { 
        XmlSerializer xs = new XmlSerializer(typeof(T)); 
        MatchCollection mc = Regex.Matches(cleanXml, @"</?(\d\w+)"); 
        List<string> elements = new List<string>(); 

        foreach (Match m in mc) 
        { 
         string cpval = m.Groups[1].Value; 
         if (!elements.Contains(cpval)) { elements.Add(cpval); } 
        } 

        foreach (string e in elements) 
        { 
         cleanXml = cleanXml.Replace(e, "d_" + e); 
        } 

        using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(cleanXml))) 
        { 
         using (StringReader sr = new StringReader(cleanXml)) 
         { 
          return (T)xs.Deserialize(sr); 
         } 
        } 
       } 
       catch(XmlException x) 
       { 
        var obj = (T)Activator.CreateInstance(typeof(T)); 
        Type type = obj.GetType(); 
        return (T)obj; 
       } 
      } 
      else 
      { 
       var obj = (T)Activator.CreateInstance(typeof(T)); 
       Type type = obj.GetType(); 
       // add in the generic derived class property search and assign 
       return (T)obj; 
      } 
     } 
+1

包含的代码,你实际上是试图将XML反序列化到所提供的对象图 - 我的第一个猜测是你错过了正确的'NamespaceManager' – Jamiec

+0

可能是(包括我使用的derserialiser),但是,我不认为我需要在解串时使用命名空间?认为这只是在发送XML时没有收到序列化。 – PurpleSmurph

+1

通过'http:// xmltocsharp.azurewebsites.net'解析你的'xml'来获得强类型的C#对象。 –

回答

0

感谢上面评论过的人 - 我终于找到了工作。删除XmlArray但不包括AnonymousIsNullable属性似乎是问题,虽然我不确定为什么它可以与我具有的所有其他功能一起使用,但也可能不需要存在serializable

工人阶级结构减去容器,并没有改变:

[Serializable()] 
    [XmlType(AnonymousType = true, Namespace = "")] 
    public class AppointmentAvailabilityStatusResult : WebserviceMessage 
    { 
     [XmlElement("DateAndTimeSlot", Namespace = "Appointments")] 
     public DateAndTimeSlot DateAndTimeSlot { get; set; } 

     [XmlElement("RequestedStatus")] 
     public int RequestedStatus { get; set; } 
    } 
    [Serializable()] 
    [XmlType(AnonymousType = true, Namespace = "Appointments")] 
    [XmlRoot(ElementName = "DateAndTimeSlot",Namespace = "Appointments", IsNullable = false)] 
    public class DateAndTimeSlot 
    { 
     [XmlElement(ElementName = "DateAndTimeslot", Namespace = "Appointments.TO")] 
     public List<DateAndTimeslot> DateAndTimeslot { get; set; } 
    } 

    [Serializable()] 
    [XmlType(AnonymousType = true, Namespace = "Appointments.TO")] 
    [XmlRoot(Namespace = "Appointments.TO", IsNullable = false)] 
    public class DateAndTimeslot 
    { 
     [XmlElement("Date")] 
     public string Date { get; set; } 
     [XmlElement("TimeSlot")] 
     public string TimeSlot { get; set; } 
    }