XMLSerializer 不转置 byte 和 int 数据类型

XMLSerializer not transposing byte and int data types

(我originally posted this in CodeExchange,但被告知放错地方了)

我有序列化我的报告的代码:

[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>();
    }
}

...和 ​​类 为我的实际报告设置

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() { }
}

..我正在正确创建我的对象:

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 = "someone@somewhere.com";
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()

...但我没有收到字节?结果输出中的数据类型:

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

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

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

请帮助我发现我做错了什么,以及为什么 Scope 和 DeviceGroup 元素不会出现在我的 XML 中?它似乎影响了字节和整数,我终究无法弄清楚为什么。

属性Scope是否序列化由ScopeSpecified属性和ShouldSerializeScope()方法控制;在你的 class 中,两者都存在,并且你没有将 ScopeSpecified 设置为 true,因此 Scope 未序列化(显然 ScopeSpecified 优先于ShouldSerializeScope())。由于您在 ShouldSerialize* 方法中实现了一些逻辑,您可能不需要 *Specified 属性。只需删除它们,它就会按预期工作。