XmlSerializer没有序列化两个枚举属性

时间:2014-01-03 09:25:09

标签: c# xml serialization xmlserializer

我们正在尝试使用XmlSerializer.Serialize将一组对象序列化为xml

我们已使用以下存根

测试了XmlSerializer
private static void TestMethod()
{
    ChartContentConfig Config = new ChartContentConfig();
    Config.DefinitionId = "6790e2ca-be93-48dd-94e7-f8ec0f6e5fd4";
    Config.Sorts = null;
    Config.DataFields = new DataField[1];
    Config.DataFields[0] = new DataField()
    {
        Aggregator = AggregateFunction.Sum,
        ApplyFilter = ApplyFilter.Before,
        FieldName = "Hello",
        FieldType = typeof(decimal).ToString(),
        FilterToValue = "",
        FilterFromValue = "",
        FilterOperator = FilterOperator.None,
        IsVisible = false,
        DisplayName = "Hello",
        DefaultStyle = "DefaultFormat"
    };

    Type configType = typeof(DataField);
    DataField DataField = Config.DataFields[0];
    DataField.MarkerFormat = "MarkerFormatTest";
    DataField.SeriesColour = "SeriesColourTest";
    DataField.TestProperty = AggregateFunction.Average;
    DataField.Aggregator = AggregateFunction.Average;
    string test = SerializeConfig(DataField, configType).InnerXml;
    System.Diagnostics.Debug.WriteLine(test);
}

xml返回正常但是每个属性都显示在Aggregator和ApplyFilter的枚举之外。由于一切似乎都很好,我们现在完全处于失败状态。以下是两者的定义。

[XmlAttribute]
public AggregateFunction Aggregator { get; set; }

[XmlAttribute]
public ApplyFilter ApplyFilter { get; set; }

带有

标签
[Serializable]
[DebuggerStepThrough]
[DesignerCategory("code")]
[GeneratedCode("xsd", "4.0.30319.1")]
[XmlRoot(Namespace = "", IsNullable = false)]

在该课程的顶部。

枚举如下所示:

[Serializable]
[GeneratedCode("xsd", "4.0.30319.1")]
public enum AggregateFunction
{
    None = 0,
    Group = 1,
    Sum = 2,
    Max = 3,
    Min = 4,
    Average = 5,
    Count = 6,
    Project = 7,
    Value = 8,
}

[Serializable]
[GeneratedCode("xsd", "4.0.30319.1")]
public enum ApplyFilter
{
    OnDisplay = 0,
    BeforeGroup = 1,
    AfterGroup = 2,
}

任何帮助都会很高兴。

谢谢,

1 个答案:

答案 0 :(得分:9)

当您使用xsd.exe从XML模式生成类时,对于使用非可空类型的每个可选属性,都会生成相应的布尔“指定”属性,该属性可用于控制是否输出该属性或者应该省略。

没有看到模式,我不能肯定地说,但我希望你的类有一个AggregateFunctionSpecified属性和一个ApplyFilterSpecified属性,需要将其设置为true才能输出属性。