Total Pageviews

Tuesday, 29 May 2012

C# XML serialization attributes

C# XML serialization attributes



Where Attribute Function
class XmlRoot name a root element
field, property, parameter, or return value XmlAttribute serialized as an XML attribute
field, property, parameter, or return value XmlElement serialized as an XML element
field, property, parameter, or return value of type XmlAttribute[] XmlAnyAttribute collects all unknown attributes
field, property, parameter, or return value of type XmlElement[] XmlAnyElement collects all unknown elements
field, property XmlIgnore not part of serialization
field, property, parameter, or return value that returns an array of complex objects XmlArray
field, property, parameter, or return value that returns an array of complex objects XmlArrayItem
field, property, parameter, or return value XmlChoiceIdentifier
field that is an enumeration identifier XmlEnum serialization of an enumeration member
derived class declarations, and return values of public methods (WSDL) XmlInclude should be included when generating schemas
properties and fields XmlText serialized as XML text
class declarations XmlType name and namespace of the XML type

Limitations of XML serialization

  • Does not handle mixed mode (all mixed items are gathered in one XmlText)
  • Serialization of interfaces (there is currently no way to specify an underlying type for deserialization)
  • Due to dynamic code generation that accesses the serialized types from outside all the members and classes to be serialized MUST be public.
Alternative: implement IXmlSerializable if you need bigger flexibility

link: http://www.color-of-code.de/index.php/cheat-sheets/programming/c-serialization-attributes

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete