12

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?

madcyree
  • 1,427
  • 3
  • 15
  • 28
  • 1
    Your question is not clear to me. Why are you not using a bool? – bnieland May 11 '11 at 11:29
  • 2
    OP wants to have the IsValid as boolean but output Y and N instead of false and true. – Aliostad May 11 '11 at 11:36
  • This is a good question about bool :) I do believe that it is a very logical decision to use a bool property, but unfortunately the xml described above comes up from external system, so I have no ability to change anything. Pretty sad, isn't it? :) – madcyree May 11 '11 at 14:06

2 Answers2

18

Treat the node as a custom type:

[XmlRoot("ObjectSummary")]
public class Summary
{
    public string Name {get;set;}
    public BoolYN IsValid {get;set;}
}

Then implement IXmlSerializable on the 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 even make that custom class a struct instead, and provide implicit conversions between it and bool to make it even more "transparent".

Jorge Poveda
  • 774
  • 9
  • 12
  • 3
    Great answer. However, I had to remove the statement writer.WriteEndElement(); to prevent crashes during the serializer.Serialize(...) operation – Hadron Nov 20 '15 at 03:08
10

The way I do it - which is suboptimal but have not found a better way - is to define two properties:

[XmlRoot("ObjectSummary")]
public class Summary
{
     public string Name {get;set;}
     [XmlIgnore]
     public bool IsValid {get;set;}
     [XmlElement("IsValid")]
     public string IsValidXml {get{ ...};set{...};}

}

Replace ... with the simple code to read and write the IsValid value to Y and N and read from it.

Aliostad
  • 80,612
  • 21
  • 160
  • 208