将 XML 反序列化为列表列表

本文关键字:列表 反序列化 XML | 更新日期: 2023-09-27 17:55:57

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

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

想要的可能吗,如果是这样,我在哪里搞砸了?

将 XML 反序列化为列表列表

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

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

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

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

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