XMLSerializer不转置byte和int数据类型

时间:2016-07-08 23:50:38

标签: c# xml serialization xml-serialization xmlserializer

(我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中?它似乎影响了字节和整数,我不能为我的生活找出原因。

1 个答案:

答案 0 :(得分:0)

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