2016-07-08 1 views
0

(je originally posted this in CodeExchange, mais on m'a dit qu'il était au mauvais endroit pour mettre cela)XMLSerializer ne pas transposer les types de données et d'octets int

J'ai le code de sérialisation mes rapports:

[XmlRoot(Namespace = "", IsNullable = false)] 
public partial class ReportSpecification{ 

    /// <remarks/> 
    [XmlElementAttribute("Reports")] 
    public ReportsHolder Reports { get; set; } 

    /// <remarks/> 
    [XmlAttributeAttribute()] 
    public decimal Version { get; set; } 

    /// <remarks/> 
    [XmlAttributeAttribute()] 
    public string Username { get; set; } 
} 

public partial class ReportsHolder{ 
    [XmlElement(IsNullable = true)] 
    public List<AlertSummary> AlertSummaryReportList { get; set; } 

    public ReportsHolder(){ 
     this.AlertSummaryReportList = new List<AlertSummary>(); 
    } 
} 

... et mis en place des classes pour mes rapports réels

public abstract partial class BaseReport{ 
    [XmlAttributeAttribute()] 
    public string ReportName { get; set; } 

    [XmlAttributeAttribute()] 
    public string FilterMode { get; set; } 

    [XmlAttributeAttribute()] 
    public string Destination { get; set; } 

    [XmlAttributeAttribute()] 
    public string Format { get; set; } 
} 

[XmlTypeAttribute(AnonymousType = true)] 
public partial class AlertSummary : BaseReport{ 
    public AlertSummaryFilters Filters; 

    private string _basicClass = "Device"; 

[XmlAttributeAttribute()] 
public string BasicClass{ 
    get { return _basicClass; } 
    set{ if (value.Length < 0) _basicClass = value; } 
} 

public AlertSummary(){ 
    Filters = new AlertSummaryFilters(); 
} 


[XmlTypeAttribute(AnonymousType = true)] 
public class AlertSummaryFilters{ 
    public string AlertSource { get; set; } 
    public byte? CriticalDevicesOnly { get; set; } 
    public byte? Scope { get; set; } 
    public ushort? DeviceID { get; set; } 
    public string DeviceType { get; set; } 
    public uint? DeviceGroup { get; set; } 
    public uint? DeviceFacility { get; set; } 
    public uint? DeviceRegion { get; set; } 

    [XmlIgnoreAttribute()] 
    public bool CriticalDevicesOnlySpecified { get; set; } 

    [XmlIgnoreAttribute()] 
    public bool ScopeSpecified { get; set; } 

    [XmlIgnoreAttribute()] 
    public bool DeviceSpecified { get; set; } 

    [XmlIgnoreAttribute()] 
    public bool DeviceGroupSpecified { get; set; } 

    [XmlIgnoreAttribute()] 
    public bool DeviceFacilitySpecified { get; set; } 

    [XmlIgnoreAttribute()] 
    public bool DeviceRegionSpecified { get; set; } 

    public bool ShouldSerializeCriticalDevicesOnly() { return CriticalDevicesOnly != null; } 

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

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

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

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

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

    internal AlertSummaryFilters() { } 
} 

..et Je crée correctement mon objet:

XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); 
ns.Add("", ""); 
XmlSerializer serializer = new XmlSerializer(typeof(Alpha.ReportSpecification)); 
TextWriter writer = new StreamWriter(@"C:\temp\AlphaTest.xml"); 

// create the root object 
Alpha.ReportSpecification myReportSpecification = new Alpha.ReportSpecification { Username = "Alpha Test", Version = (decimal)6.0 }; 

//create the report holder object 
ReportsHolder myReportsHolder = new ReportsHolder(); 

// create a test AlertSummary report 
AlertSummary myAlertSummary = new AlertSummary(); 
myAlertSummary.ReportName = "Testing AlertSummary Report from Apha"; 
myAlertSummary.FilterMode = "Container"; 
myAlertSummary.Destination = "[email protected]"; 
myAlertSummary.Format = "PDF"; 

myAlertSummary.Filters.AlertSource = "Dracula"; 
myAlertSummary.Filters.Scope = 22; 
myAlertSummary.Filters.DeviceGroup = 12; 

// add the new AlertSummary to the AlertSummary report holder 
myReportsHolder.AlertSummaryReportList.Add(myAlertSummary); 

// set the ReportSpecification report holder equal to the ReportsHolder 
myReportSpecification.Reports = myReportsHolder; 

// dump everything to the output file 
serializer.Serialize(writer, myReportSpecification, ns); 
writer.Close() 

... mais je ne reçois pas l'octet? dans la sortie des types de données résultant du tout:

<?xml version="1.0" encoding="utf-8"?> 
<ReportSpecification Version="6" Username="Alpha Test"> 
    <Reports> 
    <AlertSummaryReportList ReportName="Testing AlertSummary Report from Apha" FilterMode="Container" Destination="[email protected]" Format="PDF" BasicClass="Device"> 
     <Filters> 
     <AlertSource>Dracula</AlertSource> 

     <!-- Scope sould be here --> 
     <!-- DeviceGroup should be here --> 

     </Filters> 
    </AlertSummaryReportList> 
    </Reports> 
</ReportSpecification> 

S'il vous plaît aidez-moi à découvrir ce que je fais mal, et pourquoi la portée et les éléments devicegroup ne seront pas visibles dans mon XML? Cela semble affecter les octets et les ints, et je ne peux pas, pour la vie de moi, comprendre pourquoi.

Répondre

0

Que la propriété Scope soit sérialisée ou non est contrôlée par la propriété ScopeSpecified et la méthode ShouldSerializeScope(); dans votre classe, les deux sont présents, et vous ne définissez pas ScopeSpecified à true, donc Scope n'est pas sérialisé (apparemment ScopeSpecified a priorité sur ShouldSerializeScope()). Depuis que vous avez implémenté une certaine logique dans les méthodes ShouldSerialize*, vous ne voulez probablement pas les propriétés *Specified. Supprimez-les simplement, et cela devrait fonctionner comme prévu.

+0

Un million de remerciements pour ça! J'avais l'impression que vous aviez besoin de 'ShouldSerialize *' et '* Specified' afin de garder les choses propres et bien rangées. – Kulstad

+0

@Kulstad, non, vous n'avez besoin que d'un seul. Donner le choix entre les deux était une décision de conception malheureuse à mon avis ... –