将XML反序列化为嵌套对象列表返回计数0

时间:2016-06-07 11:27:29

标签: c# xml

我在将一些XML反序列化到对象列表时遇到一些问题,在调试期间检查DateAndTimeSlot时,总是得到计数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返回,我希望完全反序列化。

<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,如果我序列化一个虚拟对象 - 我正在尝试纠正一些差异,不确定命名空间是否是反序列化所必需的

<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 个答案:

答案 0 :(得分: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; }
    }