Suppose we have a class which can be serialized/deserialized by XmlSerializer. It would be like so:
[XmlRoot("ObjectSummary")]
public class Summary
{
public string Name {get;set;}
public string IsValid {get;set;}
}
We have an xml which will be like so:
<ObjectSummary>
<Name>some name</Name>
<IsValid>Y</IsValid>
<ObjectSummary>
Using of boolean property IsValid instead of string property is much better decision, but in this case we need to add some additional logic to convert data from string to bool.
The simple and direct way to solve this problem is to use additional property and put some conversion logic into the IsValid getter.
Can anyone suggest a better decision? To use a type converter in attributes somehow or something similar?
Treat the node as a custom type:
Then implement
IXmlSerializable
on the custom type:You can even make that custom class a
struct
instead, and provide implicit conversions between it andbool
to make it even more "transparent".The way I do it - which is suboptimal but have not found a better way - is to define two properties:
Replace ... with the simple code to read and write the IsValid value to Y and N and read from it.
If you have more than one object, put it inside a list and Serialize List.