[英]Deserialize XML to list of lists
Given the following code used to serialize: 给定以下用于序列化的代码:
[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;
}
}
Which ends up serializing like this: 最终像这样序列化:
<?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>
I am wanting to get a list of each ReportList object to process later in my application, but I am getting a "Type 'ReportSpec' is not enumerable" error in my foreach loop: 我想获取每个ReportList对象的列表,以在以后的应用程序中处理,但是在我的foreach循环中出现“ Type'ReportSpec'is not enumerable”错误:
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
}
Is what I want even possible, and if so, where am I screwing up? 我什至想要什么,如果可以,我在哪里搞砸了?
You need to enumerate the lists within the ReportSpec. 您需要枚举ReportSpec中的列表。 ie
即
foreach (var report in list.MyHolder.AlertReportsList)
{
// ...
}
foreach (var report in list.MyHolder.DeviceHistoryReportsList)
{
// ...
}
ReportSpec
is just a simple class; ReportSpec
只是一个简单的类。 And, It doesn't implement IEnumerable
interface which means you can't iterate that class using foreach
loop 而且,它没有实现
IEnumerable
接口,这意味着您不能使用foreach
循环来迭代该类。
To redirect your Reports for further processing, just use below code snippet 要重定向您的报告以进行进一步处理,只需使用以下代码段
var reportSpec= (ReportSpec)xmlSerializer.Deserialize(reader);
Processing(reportSpec.MyHolder.AlertsReportList, reportSpec.MyHolder.DeviceHistoryReportList);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.