Most elegant XML serialization of Color structure
Solution 1:
Here's something I'm using for serializing the Color
struct in XML. It's better than shadowing the primary Color
property in my opinion. Any suggestions welcome.
The XmlColor
class relies primarily on the implicit operator
language feature to provide the key data tranformations. Without this, the class is basically useless. Other bits of functionality were added to round out the class.
The XmlColor
helper also provides a convenient way to separate color components. I added the Alpha
property to show this. Notice the Alpha
component won't be serialized if it's cranked all the way up to 255.
Deserializing the Web
color value combines the Alpha
value currently stored in the instance. The order in which the attributes are parsed shouldn't matter. If the Alpha
attribute is missing in the XML source, the instance component value will be used to set the Alpha
level. This is arguably faulty; however, in the case of XML serialization, the XmlColor
class will initialized with Color.Black
setting the Alpha
to 255.
I'm working out of the VS2010 environment and building against .Net 4. I have no idea how compatible the code is with previous versions.
Here's an example property that should be serialized to XML:
[XmlElement(Type=typeof(XmlColor))]
public Color MyColor { get; set; }
Here's the XmlColor
helper class:
public class XmlColor
{
private Color color_ = Color.Black;
public XmlColor() {}
public XmlColor(Color c) { color_ = c; }
public Color ToColor()
{
return color_;
}
public void FromColor(Color c)
{
color_ = c;
}
public static implicit operator Color(XmlColor x)
{
return x.ToColor();
}
public static implicit operator XmlColor(Color c)
{
return new XmlColor(c);
}
[XmlAttribute]
public string Web
{
get { return ColorTranslator.ToHtml(color_); }
set {
try
{
if (Alpha == 0xFF) // preserve named color value if possible
color_ = ColorTranslator.FromHtml(value);
else
color_ = Color.FromArgb(Alpha, ColorTranslator.FromHtml(value));
}
catch(Exception)
{
color_ = Color.Black;
}
}
}
[XmlAttribute]
public byte Alpha
{
get { return color_.A; }
set {
if (value != color_.A) // avoid hammering named color if no alpha change
color_ = Color.FromArgb(value, color_);
}
}
public bool ShouldSerializeAlpha() { return Alpha < 0xFF; }
}
Solution 2:
I believe below I have an easier solution to that. Color serialization is ignored and color is saved and loaded as simple 32-bit ARGB data.
[XmlIgnore]
public Color BackColor { get; set; }
[XmlElement("BackColor")]
public int BackColorAsArgb
{
get { return BackColor.ToArgb(); }
set { BackColor = Color.FromArgb(value); }
}
Solution 3:
A pain, isn't it? That is all you can do with XmlSerializer
, unless you implement IXmlSerializable
(which I do not recommend). Options:
- stick with that, but also mark
color_XmlSurrogate
as[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
- that will stop it appearing in most data-binding views, and in the code-editor when referencing your assembly as a dll - use
DataContractSerializer
, which supports private properties (but which doesn't support xml attributes; you can't win...)
btw, I'd have color
as a property, not a field:
[XmlIgnore]
public Color Color {get;set;}