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.
There are additional overloads of the XmlSerializer constructor
that we have not discussed, but they merely offer different combinations of the
parameters we just learned to use. One overload combines the functionality of
all the other constructors: You can specify all the types the serializer can
serialize, attach attributes to the types the serializer can handle, specify a
root element name for each object the serializer reads or writes and define a
default namespace:
One application of this overload is to create a generic
serializer that can process many different root object types. Considering that
instantiating an XmlSerializer object is an expensive operation, we can improve
the performance of our applications by instantiating as few serializers as
possible.
We can set up such a generic serializer by specifying the object
type as the root type. The “real” types the constructor needs for setting up
the serializer instance are all specified through the extraTypes parameter.
Declaring object as the root type causes Serialize() to name the element for
each object we pass in “anyType”. That’s hardly desirable because it does not
map well to the real world, where the element names typically convey some
information. To change the element names to something more descriptive we can
either set the root parameter of the constructor, but this still only specifies
one root element name for all serialized object graphs. Populating an
XmlOverrides object, on the other hand, allows specifying a distinctive element
name for each type we serialize. The following example for a constructor
illustrates the concept of the generic serializer. This serializer instance can
serialize, otherwise unrelated, string, int and Car objects and changes the
root element name to “MyObject” for each type.
XmlSerializer serializer = new XmlSerializer( typeof(object),
null, // XmlOverrides,
use to customize root element names
new Type[] {
typeof(string), typeof(int), typeof(Car) },