c# - XmlSerializer property converter -
suppose have class can serialized/deserialized xmlserializer. so:
[xmlroot("objectsummary")] public class summary { public string name {get;set;} public string isvalid {get;set;} }
we have xml so:
<objectsummary> <name>some name</name> <isvalid>y</isvalid> <objectsummary>
using of boolean property isvalid instead of string property better decision, in case need add additional logic convert data string bool.
the simple , direct way solve problem use additional property , put conversion logic isvalid getter.
can suggest better decision? use type converter in attributes somehow or similar?
treat node custom type:
[xmlroot("objectsummary")] public class summary { public string name {get;set;} public boolyn isvalid {get;set;} }
then implement ixmlserializable
on custom type:
public class boolyn : ixmlserializable { public bool value { get; set } #region ixmlserializable members public system.xml.schema.xmlschema getschema() { return null; } public void readxml(system.xml.xmlreader reader) { string str = reader.readstring(); reader.readendelement(); switch (str) { case "y": this.value = true; break; case "n": this.value = false; break; } } public void writexml(system.xml.xmlwriter writer) { string str = this.value ? "y" : "n"; writer.writestring(str); writer.writeendelement(); } #endregion }
you can make custom class struct
instead, , provide implicit conversions between , bool
make more "transparent".
Comments
Post a Comment