public class Hat
{
[XmlTextAttribute]
public string Name { get; set; }
[XmlAttribute("Color")]
public string Color { get; set; }
}
var hat1 = new Hat {Name="Cool Hat", Color="Red"};
var hat2 = new Hat {Name="Funky Hat", Color=null};
This is what I get (notice missing color-attribute on Funky Hat):
<Hats>
<Hat Color="Red">Cool Hat</Hat>
<Hat>Funky Hat</Hat>
</Hats>
This is what I want:
<Hats>
<Hat Color="Red">Cool Hat</Hat>
<Hat Color="">Funky Hat</Hat>
</Hats>
How can I force the serializer to create an empty attribute in stead of leaving it out?
EDIT:
Turns out I am an idiot and created an example that contained an error, because I wanted to simplify the code for the example.
If the value of color is "" (or string.empty), it is actually serialized as an empty attribute. However, I really had a null value, not an empty string - hence it was left out.
So the behavior I wanted was actually already the behavior of the example I created.
Sorry, guys!