Mark Wilson I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
Modifying the default behavior of the XmlSerializer at runtime
goes further than providing type information for undeclared types. We can
perform (almost) all the same customizations we do statically through
attributes dynamically as well. The XmlSerializer class defines two constructor
overloads to override the default behavior or hard-coded attributes:
These constructors are helpful when we need to adjust static
attributes only in certain cases, or if we have to support an extended XML
format but we cannot change the attributes in the source code. The constructors
accept the overriding attributes inside a special container object of the
XmlAttributeOverrides class. This class exposes two overloaded Add() methods to
apply attributes either on a class or an individual class member:
public void Add(Type type, XmlAttributes overrides);
public void Add(Type type, string elementName,
XmlAttributes
overrides);
The type parameter identifies the class to which we want to add
serialization attributes, the elementName parameter identifies the class
member. For fields, we attached attributes for each type we expected the field
to reference at runtime. In the same vein we can assign an XmlAttributes object
for each type a field might reference.
NOTE: You need to make sure you supply the same set of
attributes if you use different instances of the XmlSerializer to serialize and
deserialize a type. This happens automatically when the attributes are attached
in source code, but not when you supply them at runtime. If you do not supply
the same set of attributes the type mappings are different and your deserializing
the XML stream might fail.
The attributes themselves are stored in a yet another container
object of the XmlAttributes class. This class exposes properties for each
metadata attribute defined in the System.Xml.Serialization namespace, e.g. an
XmlElements collection for XmlElementAttributes, an XmlRoot property for an
XmlRootAttributes and so on. The only attribute we can not attach at runtime is
the XmlIncludeAttribute, i.e. we cannot declare type substitutions at runtime.
We can, however, declare types globally as we have seen in section 10.2.1.1 or
declare substitute types for individual fields by dynamically attaching
XmlElement or XmlArrayItem attributes.
1.4 Table 10.2 The
properties of the XmlAttributes class store serialization attributes to
customize the XmlSerializers class-to-type mapping at runtime.
Property
Type
Access
Description
XmlAnyAttribute
XmlAnyAttributeAttribute
read/write
Contains the
XmlAnyAttributeAttribute object marking field with literal XML attributes
XmlAnyElements
XmlAnyElementAttributes
read/write
Contains the
XmlAnyElementAttributes collection marking fields with literal XML elements
XmlArray
XmlArrayAttribute
read/write
Contains the
XmlArrayAttribute defining the name of an array root element.
XmlArrayItems
XmlArrayItemAttributes
read/write
Contains the
XmlArrayItemAttributes collection defining properties of array item elements.
XmlAttribute
XmlAttributeAttribute
read/write
Contains an
XmlAttribute object defining the XML attribute properties of a field.
XmlChoiceIdentifier
XmlChoiceIdentifierAttribute
read/write
Contains an
XmlChoiceIdentifier referring to a field that identifies a choice particle.
XmlDefaultValue
Object
read/write
Contains the
default value for an XML element or attribute.
XmlElements
XmlElementAttributes
read/write
Contains a
collection of XmlElement objects defining the XML element properties of a
field.
XmlEnum
XmlEnumAttribute
read/write
Contains an
XmlEnumAttribute defining the enumeration properties of a field.
XmlIgnore
Boolean
read/write
Contains a flag
whether or not to ignore a public field for serialization and
deserialization.
Xmlns
Boolean
read/write
XmlRoot
XmlRootAttribute
read/write
Contains an XmlRoot
object defining properties of the root element if an object is at the top of
a serialization hierarchy
XmlText
XmlTextAttribute
read/write
Contains an XmlText
object marking a field as the container for XML text
XmlType
XmlTypeAttribute
read/write
Contains an XmlType
object explicitly defining the corresponding XML schema type of a class
The constructor of the XmlSerializer throws an
InvalidOperationException if the overriding attributes are incompatible with
the type. For example, the XmlSerializer does not support dynamically adding an
XmlArrayItem attribute to arrays or collections at the root of the serialized
object graph.