This site has been taken over by the staff of www.ASPDeveloper.Net

Please report errors to suggest@aspdeveloper.net

BizTalk Utilities CV ,   Jobs ,   Code library  
 
Home Page
XmlSerializer
Customizing Xml Serialization
Advanced XmlSerializer
XmlSerializer Attributes
Runtime exceptions
Serializing Collection Classes
Serializing Arrays
XmlElement Attribute Type
XmlIncludeAttribute
XmlSerializer
Serialization Attributes
Serializable Classes
Deserializing an object
Serializing an object
Serialize Class
Metadata Attributes
<< XML DOM
XQuery >>

By :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.
First posted :03/24/2008
Times viewed :3443

 

Runtime Customization

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:

XmlSerializer(Type types, XmlAttributeOverrides overrides);

XmlSerializer(Type type, XmlAttributeOverrides overrides,

  Type[] extraTypes, XmlRootAttribute root, string defaultNamespace);

XmlAttributeOverrides Class

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 XmlAttributes Class

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 XmlSerializer’s 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.


Rate this article on a scale of 1 to 10

Your vote :  


 

Recent Jobs

Software Specialist, Linux - Finlan
Linux Core Technical Project Manage
Graphics designer at Tanzania. Expe
Integration Specialist Needed - Wor
Virtualization Server Infrastructur

View all Jobs (Add yours)
View all CV (Add yours)






    Email TopXML  

Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP