6

We are currently trying to serialize a set of objects to xml using the XmlSerializer.Serialize

We have tested the XmlSerializer with the following stubs

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

The xml returns fine however each attribute appears apart from the enums of Aggregator and ApplyFilter. We are now completely at loss for what is causing this as everything seems fine. Here are the definitions for both below.

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

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

with the tags of

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

at the top of that class.

The enums appear as follows:

[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,
}

and

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

Any help would be gladly appreciated.

Thanks,

Matt.

Matt W
  • 323
  • 3
  • 14
  • If there is anything else required I'm missing to include, please let me know. – Matt W Jan 03 '14 at 09:41
  • To learn how to attribute types and members for XML serialization, I find using `xsd.exe` with a well-formed XML schema to generate to a C# class structure the easiest. BTW `[Serializable]` is only applicable to binary serialization. – leppie Jan 03 '14 at 10:44
  • Could [this SO question](http://stackoverflow.com/questions/3890728/xml-serialization-of-enums) be relevant? Are there any AggregateFunctionSpecified or ApplyFilterSpecified properties? – Polly Shaw Jan 03 '14 at 14:10
  • Fixed this about half an hour ago, turns out that was exactly the answer. So thank you very much. Post that as the answer and I'll flag it for you if you want? – Matt W Jan 03 '14 at 14:14

1 Answers1

17

When you use xsd.exe to generate a class from an XML schema, for every optional property that uses a non-nullable type a corresponding boolean 'Specified' property is generated, which can be used to control whether that property is output or should be omitted.

Not having seen the schema, I can't say for sure, but I expect that your class has an AggregateFunctionSpecified property and an ApplyFilterSpecified property which need to be set to true in order to output the attributes.

Polly Shaw
  • 2,932
  • 1
  • 15
  • 21
  • 2
    I have spent an entire work day on this issue! Thank you! – Viktor Aug 17 '17 at 13:02
  • 2
    Thanks for this, in mine it was a property suffixed with ....FieldSpecified I just set that to true and it worked. So it was myPropFieldSpecified = true; – Choco Nov 16 '17 at 05:58