15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

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

934 ❘ ChaPTer 33 mAnipulAtinG xml<br />

Looking at the list box, you can check that in the data tables that were created all the columns have the<br />

correct names <strong>and</strong> data types.<br />

Something else you might want to note is that, because the previous two examples did not transfer any data<br />

to or from a database, no SqlDataAdapter or SqlConnection was defi ned. This shows the real fl exibility<br />

of both the System.Xml namespace <strong>and</strong> ADO.<strong>NET</strong>: you can look at the same data in multiple formats.<br />

If you need to do a transform <strong>and</strong> show the data in HTML format, or if you need to bind the data to a grid,<br />

you can take the same data <strong>and</strong>, with just a method call, have it in the required format.<br />

serializing objeCTs in Xml<br />

Serializing is the process of persisting an object to disk. Another part of your application, or even a separate<br />

application, can deserialize the object, <strong>and</strong> it will be in the same state it was in prior to serialization. The<br />

.<strong>NET</strong> Framework includes a couple of ways to do this.<br />

This section looks at the System.Xml.Serialization namespace, which contains classes used to serialize<br />

objects into XML documents or streams. This means that an object ’ s public properties <strong>and</strong> public fi elds are<br />

converted into XML elements, attributes, or both.<br />

The most important class in the System.Xml.Serialization namespace is XmlSerializer . To serialize<br />

an object, you fi rst need to instantiate an XmlSerializer object, specifying the type of the object to<br />

serialize. Then you need to instantiate a stream/writer object to write the fi le to a stream/document. The<br />

fi n a l s t ep i s to c a l l t he Serialize() method on the XMLSerializer , passing it the stream/writer object <strong>and</strong><br />

the object to serialize.<br />

Data that can be serialized can be primitive types, fi elds, arrays, <strong>and</strong> embedded XML in the form of<br />

XmlElement <strong>and</strong> XmlAttribute objects.<br />

To deserialize an object from an XML document, you reverse the process in the previous example.<br />

You create a stream/reader <strong>and</strong> an XmlSerializer object <strong>and</strong> then pass the stream/reader to the<br />

Deserialize() method. This method returns the deserialized object, although it needs to be cast to<br />

the correct type.<br />

The XML serializer cannot convert private data, only public data, <strong>and</strong> it cannot<br />

serialize object graphs. However, these should not be serious limitations; by carefully<br />

designing your classes, you should be able to easily avoid these issues. If you do need to<br />

be able to serialize public <strong>and</strong> private data as well as an object graph containing many<br />

nested objects, you will want to use the System.Runtime.Serialization<br />

.Formatters.Binary namespace.<br />

Some of the other tasks that you can accomplish with System.Xml.Serialization classes are:<br />

➤<br />

➤<br />

➤<br />

Determine if the data should be an attribute or element<br />

Specify the namespace<br />

Change the attribute or element name<br />

The links between your object <strong>and</strong> the XML document are the custom <strong>C#</strong> attributes that annotate your<br />

classes. These attributes are what are used to inform the serializer how to write out the data. The xsd.exe<br />

tool, which is included with the .<strong>NET</strong> Framework, can help create these attributes for you. xsd.exe can do<br />

the following:<br />

➤<br />

➤<br />

Generate an XML schema from an XDR schema fi le<br />

Generate an XML schema from an XML fi le<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!