I would like to know how to ignore a specific item/index of a List<T>
from being serialized using XmlSerializer
.
For example, consider the following list:
...
List<int> collection = new List<int>() {0, 1, 2};
...
What I would like to achieve is when serializing the above List<int>
using XmlSerializer
, I want the 0
to be ignored from being serialized, so the desired result is:
...
<collection>
<int>1</int>
<int>2</int>
</collection> // as you can see - there is no <int>0</int> value.
...
Thanks.
UPDATE
The following code is a concrete example of my question:
[Serializable]
public class Ball
{
private static readonly XmlSerializer Serializer = new XmlSerializer(typeof(Ball));
public Ball()
{
// value 1 is a default value which I don't want to be serialized.
Points = new List<int>() { 1 };
IsEnabled = false;
}
public List<int> Points { get; set; }
public bool IsEnabled { get; set; }
public void Save()
{
using (StreamWriter writer = new StreamWriter(FILENAME))
{
Serializer.Serialize(writer, this);
}
}
public Ball Load()
{
using (StreamReader reader = new StreamReader(FILENAME))
{
return (Ball)Serializer.Deserialize(reader);
}
}
}