0
[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="[email protected]" Format="PDF">
<Filters>
<AlertSource>Frankenstein</AlertSource>
<Scope>0</Scope>
</Filters>
</AlertsReportList>
<AlertsReportList FilterMode="Container" Destination="[email protected]" 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="[email protected]" Format="TXT">
<Filters>
<DeviceID>44</DeviceID>
</Filters>
</DeviceHistoryReportList>
</Reports>
</ReportSpec>
我希望得到各ReportList對象的一個列表過程稍後在我的應用程序,但我在我的foreach循環中得到一個「Type」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
}
是我想甚至可能的,如果是這樣,我在哪裏搞砸了?
有實施辦法一個基於'report.GetType()'的'switch'語句,還是我正在考慮重新設計我的序列化程序(我目前並不反對)? – Kulstad
不知道爲什麼你需要這樣做 - 你能澄清一下嗎?上面的循環變量報告應該是第一個循環中的AlertReport類型和第二個循環中的DeviceHistoryReport類型。 –
也許這是個人的事情。我目前有12種不同類型的報表需要處理(所以現在有AlertsReportsList和DeviceHistoryReportsList,現在有更多的報表需要添加),並且使用'foreach'語句對我來說比我看起來更「笨重」 'switch'語句。是的,他們完成了同樣的事情,但'switch'總是比我更光滑 - 感覺對我來說(除非有比foreach或switch更好的方法)。 – Kulstad