BizTalk Utilities CV ,   Jobs ,   Code library  
 
Home Page
XmlSerializer
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 :2845

 

Deserializing an object

Before we look at the

Serialize() method more in depth I will show you that de-serializing an object also works with two lines of code:

Listing 9.1: Using the Deserialize() method to read an XmlDocuement

void DeserializeACar()

{

  XmlSerializer ser = new XmlSerializer(typeof(Car));

  XmlTextReader reader = new XmlTextReader("car.xml");

  Car wifesCar = (Car)ser.Deserialize(reader);

  // wifesCar is a 1997 Ford Explorer again

  reader.Close();

}

This time we instantiate an XmlTextReader for the XML file we created in the previous example. Then we pass it to the Deserialize() method and the XmlSerializer determines that it 1) needs to instantiate a new Car object, 2) creates a new Car object and 3) sets the Car object’s fields to the values supplied by the XmlReader before it returns the new Car object to you. Note that the type cast of the Deserialize() method’s return value to Car is necessary because the Deserialize() is declared to return only an object and the assignment would not be valid without the cast.

Imagine you are developing an application which receives large XML documents with nested types and hundreds of nodes. How many lines of code would you have to write to parse these documents and assign their content to an object hierarchy for further processing? With XML data binding you define classes that map to the XML format of the documents you are receiving and let the XmlSerializer do the mapping. Instead or writing hundreds of lines of code you get by writing four.  Starting with section 9.2.2 we will learn how put this powerful functionality to work to bind objects to arbitrarily complex XML formats.

Serialize() and Deserialize() methods

Both methods, Serialize() and Deserialize() leverage the concepts of streams and abstract base classes we learned about in chapter 2. Several overloads shown in table 9.1 and table 9.2 are available for both methods to allow dealing with XML from a variety of sources.

Table 9.1: Available overloads for the Serialize() method. Each overloaded method allows different options to control encoding and formatting of the output. Some overloads also allow the declaration of XML namespaces used in the created XML document.

Serialize overload

Purpose

public void Serialize(Stream stream, object o);

Serialize an object to any kind of stream. The XML written to the stream is always UTF-8 encoded and formatted with indentations.

public void Serialize(Stream stream, object o, XmlSerializerNamespaces namespaces);

Serialize an object to any kind of stream and declare XML namespace prefixes to use throughout the generated MXL document. The XML written to the stream is always UTF-8 encoded and formatted with indentations.

public void Serialize(TextWriter writer, object o);

Serialize an object to a TextWriter. The TextWriter controls encoding of the generated XML document. The document is always formatted with indentations.

public void Serialize(TextWriter writer, object , XmlSerializerNamespaces namespaces o);

Serialize an object to a TextWriter and declare XML namespace prefixes to use throughout the generated XML document. The TextWriter controls encoding of the generated XML document. The document is always formatted with indentations.

public void Serialize(XmlWriter writer, object o);

Serialize an object to an XmlWriter. The XmlWriter controls encoding and formatting of the generated XML document.

public void Serialize(XmlWriter writer, object o, XmlSerializerNamespaces namespaces);

Serialize an object to an XmlWriter and declare XML namespace prefixes to use throughout the generated XML document. The XmlWriter controls encoding and formatting of the generated XML document.

The XmlSerializer always operates on the current position of the input or output stream. Using raw streams you can even inject and retrieve XML from any kind of data stream, not only a well-formed XML document.

Table 9.2: The available overloads for the Deserialize() method allow to deserialize objects from a variety of sources: file-based, memory-based and over a network.

Deserialize Overload

Purpose

 public object Deserialize(Stream stream);

Deserialize an object graph from any kind of stream.

 public object Deserialize(TextReader reader);

Deserialize an object graph from a TextReader.

 public object Deserialize(XmlReader reader);

Deserialize an object graph from a XmlReader.


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)



Information Online

swimming pool builder
chicago web site design
teleconferencing service
Domain Names
fax.com
Fendi 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