Hi,
I´m trying to serialize objects in C# to Xml.
Is there a way, only to serialize a property if its value is different to null or nothing?
So, it its value is null, then not to serialize.
Thanks!
Printable View
Hi,
I´m trying to serialize objects in C# to Xml.
Is there a way, only to serialize a property if its value is different to null or nothing?
So, it its value is null, then not to serialize.
Thanks!
Sorry I forgot to mention, that the property is returning a List.
So by default it always returns a set of outer tags.
For instance;
Even though the List<cars> is empty, it always returns a set of outer tags.Code:public class cars
{
public cars()
{
}
public cars(string carName)
{
this.CarName = carName;
this.carList.Add(testCar);
}
string name;
List<cars> carList = new List<cars>();
[System.Xml.Serialization.XmlArrayAttribute("CarListTest")]
public List<cars> carlist
{
get
{
return this.carList;
}
}
}
What I´d like is, that if List<cars> is empty, then not to export no xml, not even outer tags.
Amazing but true,
I really do not know why this works!
But if you add the following method;
In this case;Code:public bool ShouldSerialize<property>
{
return carList.Count > 0;
}
It will only serialize carlist if it has a count>0Code:public bool ShouldSerializecarlist()
{
return carList.Count > 0;
}