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

 

Serialize Class

The XmlSerializer is your new best friend when it comes to serializing objects to XML documents. It will save you from writing code using with the XmlDocument or XmlTextReader/-Writer classes to save and restore objects. Many of us are probably familiar developing serialization solutions using an XML parser directly, e.g. if you have an application sending the data over the internet. In your application, the data you want to send is lives encapsulated within objects, but you need to serialize the objects in order to send the data. You probably added boiler-pate code to these classes to persist fields to XML using a DOM Document class. If your application also received data in XML format you probably wrote more lines of boiler-plate code to parse the received XML and set the properties on your objects.

Now the XmlSerializer does it all for you. It handles the transformation both ways and, to make life even better, we do not even have to create the classes to serialize ourselves. If we have an XML schema describing the data layout of our transfer format we can generate .NET classes corresponding to the complex types in the schema with the Framework’s XSD schema definition tool. You can find all about the XSD tool in Appendix C. In the following sections of this chapter we will learn how to use the XmlSerializer, and how we need to design classes so the XmlSerializer can use them.

9.2.1 Round-trip serialization

Let’s start out developing a class for the XmlSerializer to process. The garage example document used throughout the first part of this book contained car element nodes with attributes to store some information about a car. That’s a good candidate for a class right there.

When we develop .NET classes we no longer need to worry about writing methods to serialize an object to XML. This functionality is built directly into the .NET Framework. Let’s start with a very simple

Car class. Each Car object needs members for make, model and the year the car was built.

Listing 9.1: example class to use with the XmlSerializer

public class Car

{

  public Car(){}

  public string Make;

  public string Model;

  public int Year;

}

This class is very straightforward, all the data stored is exposed through public fields. OK, exposing fields like this is generally bad design, but 1) this is an only example and 2) there is a reason why this example class declares its members public. We will hear more about that reason shortly. In chapters 12-14 we will also meet completely different approach to object serialization that in fact can serialize non-public members. Also notice that the class does not contain any methods or derive from any base classes to enable serialization.


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 contractor
chicago web site design
saveonconferences uk rates
Web Hosting
bigfoot conferencing
Burberry 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