1

I love automatic xml serialization of public members in C#. It saves you so much time. But now I may have reached the limits, or do you know how to do that?

What I want to achieve is that nothing is written for a member when it has a default value. That is is easy with a standard type like "int" for example:

[XmlElement(ElementName = "Test", IsNullable = false), DefaultValue(0)]
public int Test = 0;

So as long as "Test" equals 0 nothing is written on serialization.

But what about structs as members? For example:

    public struct Vector2 {
        public float x;
        public float y;

        public Vector2(float x = 0.0f, float y = 0.0f) {
           this.x = x;
           this.y = y;
        }
        ...
    }

    public class ToSerialize {
        [XmlElement(ElementName = "Shift", IsNullable = false), DefaultValue(new Vector2(0,0))]
        public Vector2 Shift = new Vector2(0, 0);
        ...
    }

This does not work because "new Vector2(0,0)" is not a const. Maybe I am just unable to figure out that attribute syntax. How can I make it work? This question is about coding comfort, so I am not interested in solutions like "write your own serialization code" or other proposals which result in lengthy code. If you know that this does not work with the automatic serialization system that would be an answer too.

Ole Dittmann
  • 1,764
  • 1
  • 14
  • 22

2 Answers2

1

In such cases you can use a trick to serialize another type.

[XmlIgnore]
public Vector2 Shift = new Vector2(0, 0);

[XmlElement("Shift")] // to have same name as original field/property
public string _Shift
{
    get { return ...; } // convert Vector2 to string
    set { Shift = ... value; } // opposite
}

You will have to choose type (usually string is the most appropriate) to convert into/from.

Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • I used that trick on some occasions. But on more complicated structs which may have their own serialization properties it is not very practicable to convert them to a string. – Ole Dittmann Dec 11 '14 at 14:05
  • You can convert them into `xml` then, which then will be a string to serialize with `class ToSerialize`). Point is you are able to define *default* value, because it is what `Shift` will have (if `Shift` is missing in xml, then `_Shift` setter is not called). – Sinatr Dec 11 '14 at 14:09
0

your answer is most probably in this post.

It explain how to create a boolean method that returns whether that element should be serialized.

Community
  • 1
  • 1
Adrian Nasui
  • 1,054
  • 9
  • 10
  • Answers should be largely self-contained - this provides *no* useful information without following the link. You should extract a relevant summary into your answer... or just add the link as a comment on the question instead. – Jon Skeet Dec 11 '14 at 13:27
  • Nevertheless the perfect answer is there, thank you. – Ole Dittmann Dec 11 '14 at 14:03
  • thanks @JonSkeet for the heads up, still learning the ropes. best, A – Adrian Nasui Dec 11 '14 at 14:04