C# System.Xml doesn't serialize tag if the name is "value"

Viewed 30

C# System.Xml doesn't serialize tag if the name is "value". Is it feature or bug?

[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
public System.Nullable<decimal> value {
    get {
        return this.valueField1;
    }
    set {
        this.valueField1 = value;
    }
}

Setting the request:

filterGroup.numberFilters[0] = new numberFilter() {
            field = "ID",
            operation = numberOperation.EQUAL,
            value = 99
        };

The serialized request:

<numberFilters>
  <field>ID</field>
  <operation>EQUAL</operation>
</numberFilters>

If I change this tag name to something other, the result is correct

<numberFilters>
  <field>ID</field>
  <operation>EQUAL</operation>
  <valueKA>99</valueKA>
</numberFilters>
1 Answers

Your property definition looks wonky. "value" is generally a reserved keyword in the setter. Suggest you rename public property to Value, and keep backing field valueField1.

[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
public System.Nullable<decimal> value { <-- this 
    get {
        return this.valueField1;
    }
    set {
        this.valueField1 = value; <-- and this cant be the same.
    }
}

Related