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?
The error means exactly what it says. You cannot use these XML serialization attributes when IXmlSerializable is implemented because IXmlSerializable expects the XML serialization to be completely customized. You can do this if you want to make the class serializable with XmlSerializer using the attributes.
Also, for your implementation of XmlSerializable:
If, on the other hand, all you are looking to be able to do is have a short string represenation of a color that is reversible, have a look at the ColorTranslator Class. In particular, see the FromHtml and ToHtml methods.