One problem bugged me enough to register on Stack Overflow. Currently if I want to serialize Color to XML string as named color, or #rrggbb
, or #aarrggbb
, I do it like this:
[XmlIgnore()] public Color color; [XmlElement(ElementName = "Color")] public String color_XmlSurrogate { get { return MyColorConverter.SetColor(color); } set { color = MyColorConverter.GetColor(value); } }
Here MyColorConverter
does serialization just the way I like it. But all this feels like a kludge, with additional field and all. Is there a way to make it work in less lines, maybe connecting TypeDescriptor with C# attributes related to XML?
XML Serialization Considerations Type identity and assembly information are not included. Only public properties and fields can be serialized. Properties must have public accessors (get and set methods). If you must serialize non-public data, use the DataContractSerializer class rather than XML serialization.
Xml Serializer serializes only public member of object but Binary Serializer serializes all member whether public or private. In Xml Serialization, some of object state is only saved but in Binary Serialization, entire object state is saved.
XML serialization is the process of converting XML data from its representation in the XQuery and XPath data model, which is the hierarchical format it has in a Db2® database, to the serialized string format that it has in an application.
There are three types of serialization in . Net : Binary Serialization, SOAP Serialization and XML Serialization.
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; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With