Is there a straight forward way to [De]serialize a bool field as an element being present/not present
Assume I have a class like this:
[Serializable]
pub开发者_开发知识库lic class Person
{
public string Name { get; set; }
public int Age { get; set; }
public bool Deleted { get; set; }
}
I would like the output to be:
<Person>
<Name>Thomas</Name>
<Age>33</Age>
<Deleted />
</Person>
or
<Person>
<Name>Thomas</Name>
<Age>33</Age>
</Person>
Depending on the setting of .Deleted = true|false.
Check out this answer...
Basically, you want to use the XmlSerializer ShouldSerialize. Note that in this case, you'll probably end up with a true tag, instead of just the presence of the tag; you might have to do some more workaround if that's not good.
精彩评论