Вопрос

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.

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top