How to use a custom IXmlSerializable as an XmlAttribute?

Viewed 7096

I implement IXmlSerializable for the type below which encodes a RGB color value as a single string:

public class SerializableColor : IXmlSerializable
{
    public int R { get; set; }
    public int G { get; set; }
    public int B { get; set; }

    public XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(XmlReader reader)
    {
        var data = reader.ReadString();
        reader.ReadEndElement();
        var split = data.Split(' ');
        R = int.Parse(split[0]);
        G = int.Parse(split[1]);
        B = int.Parse(split[2]);
    }

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteString(R + " " + G + " " + B);
    }
}

Since it's a single string, I wanted to store it as an attribute to save space. But as soon as I add the [XmlAttribute] to my property, I get the following exception:

{"Cannot serialize member 'Color' of type SerializableColor. XmlAttribute/XmlText cannot be used to encode types implementing IXmlSerializable."}

Is there a way to make it work as an attribute too?

2 Answers
Related