BizTalk Utilities CV ,   Jobs ,   Code library  
 
Home Page
XmlSerializer
Runtime Overrides
Runtime Customization
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 :1049

 

Generic XmlSerializer

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:

XmlSerializer(Type type, XmlAttributeOverrides overrides,

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

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) },

  new XmlRootAttribute( "MyObject" ),

  null ); // XML namespace


Rate this article on a scale of 1 to 10

Your vote :  


 

Recent Jobs

A great opportunity to Digital Vide
here is a greate opportunity as a S
A great opportunity as a Network En
A Greate Opportunituy as a SQL Deve
An immediate job opportunity as a B

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



swimming pool contractor
chicago web site design
conference call
Web Hosting
gotomeeting
designer sunglasses
answering service


    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