【问题标题】:XMLSerializer not transposing byte and int data typesXMLSerializer 不转置 byte 和 int 数据类型
【发布时间】:2016-07-08 23:50:38
【问题描述】:

(我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 中?它似乎影响了字节和整数,我一生都无法弄清楚为什么。

【问题讨论】:

    标签: c# xml serialization xml-serialization xmlserializer


    【解决方案1】:

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

    【讨论】:

    • 一百万谢谢你!我的印象是你需要ShouldSerialize**Specified 才能保持整洁。
    • @Kulstad,不,您只需要其中一个。在我看来,在两者之间做出选择是一个不幸的设计决定......
    猜你喜欢
    • 1970-01-01
    • 2016-06-25
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    相关资源
    最近更新 更多