将XML反序列化为列表列表

时间:2016-07-08 14:22:23

标签: c# xml deserialization

给出以下用于序列化的代码:

[XmlRoot(Namespace = "", IsNullable = true, ElementName = "ReportSpec")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ReportSpec{
    [System.Xml.Serialization.XmlElementAttribute("Reports")]
    public ReportsHolder MyHolder { get; set; }

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public decimal Version { get; set; }

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Username { get; set; }
}

public partial class ReportsHolder{
    [System.Xml.Serialization.XmlElement(IsNullable = true)]
    public List<AlertsReport> AlertsReportList { get; set; }

    [System.Xml.Serialization.XmlElement(IsNullable = true)]
    public List<DeviceHistoryReport> DeviceHistoryReportList { get; set; }

    public ReportsHolder(){
        this.AlertsReportList = new List<AlertsReport>();
        this.DeviceHistoryReport = new List<DeviceHistoryReport>();
    }
}

public abstract class BaseReport{
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string ReportName { get; set; }

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string FilterMode { get; set; }

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Destination { get; set; }

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Format { get; set; }
}

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class AlertsReport : BaseReport{

    public AlertsReportFilters Filters { get; set; }

    public AlertsReport(){
        Filters = new AlertsReportFilters();
    }
}

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class AlertsReportFilters{

    public string AlertSource { get; set; }

    public byte? Scope { get; set; }

    public bool ShouldSerializeScope(){
        return Scope != null;
    }

    public ushort? DeviceID { get; set; }

    public bool ShouldSerializeDeviceID(){
        return DeviceID != null;
    }

    public string DeviceType { get; set; }

    public byte? DeviceGroup { get; set; }

    public bool ShouldSerializeDeviceGroup(){
        return DeviceGroup != null;
    }

    public uint? DeviceFacility { get; set; }

    public bool ShouldSerializeDeviceFacility(){
        return DeviceFacility != null;
    }

    public uint? DeviceRegion { get; set; }

    public bool ShouldSerializeDeviceRegion(){
        return DeviceRegion != null;
    }
}

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class DeviceHistoryReport : BaseReport{

    public ushort? DeviceID { get; set; }

    public bool ShouldSerializeDeviceID(){
        return DeviceID != null;
    }
}

最终会像这样序列化:

<?xml version="1.0" encoding="utf-8"?>
<ReportSpec Version="4.5" Username="My Name">
  <Reports>
    <AlertsReportList FilterMode="Container" Destination="someone@somewhere.com" Format="PDF">
      <Filters>
        <AlertSource>Frankenstein</AlertSource>
        <Scope>0</Scope>
      </Filters>
    </AlertsReportList>

    <AlertsReportList FilterMode="Container" Destination="someone.else@somewhere.com" Format="XLS">
      <Filters>
        <DeviceGroup>12</DeviceGroup>
      </Filters>
    </AlertsReportList>

    <DeviceHistoryReportList FilterMode="Container" Destination="\\path\on\my\network" Format="DOC">
      <Filters>
        <DeviceID>255</DeviceID>
      </Filters>
    </DeviceHistoryReportList>

    <DeviceHistoryReportList FilterMode="Container" Destination="mr.executive@somewhere.com" Format="TXT">
      <Filters>
        <DeviceID>44</DeviceID>
      </Filters>
    </DeviceHistoryReportList>
  </Reports>
</ReportSpec>

我想获取每个ReportList对象的列表,以便稍后在我的应用程序中处理,但是我的foreach循环中出现“类型'ReportSpec'不可枚举”错误:

var streamReader = new StreamReader(@"C:\temp\TestFile.xml");
TextReader reader = streamReader;
var xmlSerializer = new XmlSerializer(typeof(ReportSpec));
var list = (ReportSpec)xmlSerializer.Deserialize(reader);
foreach (var report in list){    // <-- error is here
    //re-direct the report (AlertsReportList, DeviceHistoryReportList) for processing
}

我想要的是什么,如果是的话,我在哪里搞砸了?

2 个答案:

答案 0 :(得分:0)

您需要枚举ReportSpec中的列表。即。

foreach (var report in list.MyHolder.AlertReportsList)
{
    // ...
}

foreach (var report in list.MyHolder.DeviceHistoryReportsList)
{
    // ...
}

答案 1 :(得分:0)

ReportSpec只是一个简单的类;并且,它没有实现IEnumerable接口,这意味着您无法使用foreach循环迭代该类

要重定向报告以进行进一步处理,只需使用以下代码段

即可
var reportSpec= (ReportSpec)xmlSerializer.Deserialize(reader);
Processing(reportSpec.MyHolder.AlertsReportList, reportSpec.MyHolder.DeviceHistoryReportList);