13.07.2015 Views

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

eferenced assembly and its properties configured with the stored <strong>in</strong><strong>for</strong>mation. Theserialization process needs mapp<strong>in</strong>g <strong>in</strong><strong>for</strong>mation rather than type <strong>in</strong><strong>for</strong>mation.That said, you can see <strong>in</strong> the preced<strong>in</strong>g list<strong>in</strong>g that the ArrayList object is serialized withtype <strong>in</strong><strong>for</strong>mation <strong>in</strong> the node. This happens because the ArrayList classmanages generic object references, whereas concrete types are needed <strong>for</strong>serialization and deserialization. To <strong>for</strong>ce .<strong>NET</strong> Framework <strong>for</strong>-matters to <strong>in</strong>clude type<strong>in</strong><strong>for</strong>mation, you simply set the TypeFormat property of the serializer. Let's look at howto accomplish this with the <strong>XML</strong> serializer.Add<strong>in</strong>g Type In<strong>for</strong>mationOne of the constructors of the XmlSerializerclass takes a second argument of typeXmlTypeMapp<strong>in</strong>g. The XmlSerializer class is used to encode and serialize an object toSOAP. The follow<strong>in</strong>g code is used to add XSD type def<strong>in</strong>itions to a serialized class:SoapReflectionImporter imp = new SoapReflectionImporter();XmlTypeMapp<strong>in</strong>g tm = imp.ImportTypeMapp<strong>in</strong>g(typeof(Employee));XmlSerializer ser = new XmlSerializer(tm);Let's assume the follow<strong>in</strong>g class def<strong>in</strong>ition:public class Employee{public <strong>in</strong>t ID;public str<strong>in</strong>g FirstName;public str<strong>in</strong>g LastName;}The typed <strong>XML</strong> output looks like this:4D<strong>in</strong>oEspositoThe f<strong>in</strong>al output gets a bit more complicated if custom types are <strong>in</strong>volved. For example,consider the follow<strong>in</strong>g nested classes:public class Employee{public <strong>in</strong>t ID;public str<strong>in</strong>g FirstName;public str<strong>in</strong>g LastName;public Order LastOrder;}public class Order{402

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!