2016-07-08 83 views
0

的名單:反序列化XML來考慮用於序列化下面的代碼清單

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

是我想甚至可能的,如果是這樣,我在哪裏搞砸了?

回答

0

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

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

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

有實施辦法一個基於'report.GetType()'的'switch'語句,還是我正在考慮重新設計我的序列化程序(我目前並不反對)? – Kulstad

+0

不知道爲什麼你需要這樣做 - 你能澄清一下嗎?上面的循環變量報告應該是第一個循環中的AlertReport類型和第二個循環中的DeviceHistoryReport類型。 –

+0

也許這是個人的事情。我目前有12種不同類型的報表需要處理(所以現在有AlertsReportsList和DeviceHistoryReportsList,現在有更多的報表需要添加),並且使用'foreach'語句對我來說比我看起來更「笨重」 'switch'語句。是的,他們完成了同樣的事情,但'switch'總是比我更光滑 - 感覺對我來說(除非有比foreach或switch更好的方法)。 – Kulstad

0

ReportSpec只是一個簡單的類;而且,它不實現IEnumerable接口,這意味着使用foreach循環

要重定向您作進一步處理報告,則不能重複這個類,只需使用下面的代碼片段

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