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.

serializing objects in XMl ❘ 935<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

Generate DataSet classes from an XSD schema file<br />

Generate runtime classes that have the custom attributes for XmlSerialization<br />

Generate an XSD file from classes that you have already developed<br />

Limit which elements are created in code<br />

Determine which programming language the generated code should be in (<strong>C#</strong>, Visual Basic .<strong>NET</strong>, or<br />

JScript .<strong>NET</strong>)<br />

Create schemas from types in compiled assemblies<br />

You should refer to the Framework documentation for details of comm<strong>and</strong>-line options for xsd.exe.<br />

Despite these capabilities, you don’t have to use xsd.exe to create the classes for serialization. The process<br />

is quite simple. The following is a simple application that serializes a class. At the beginning of the example,<br />

you have very simple code that creates a new Product object, pd, <strong>and</strong> fills it with some data:<br />

private void button1_Click(object sender, EventArgs e)<br />

{<br />

//new products object<br />

Product pd = new Product();<br />

//set some properties<br />

pd.ProductID = 200;<br />

pd.CategoryID = 100;<br />

pd.Discontinued = false;<br />

pd.ProductName = "Serialize Objects";<br />

pd.QuantityPerUnit = "6";<br />

pd.ReorderLevel = 1;<br />

pd.SupplierID = 1;<br />

pd.UnitPrice = 1000;<br />

pd.UnitsInStock = 10;<br />

pd.UnitsOnOrder = 0;<br />

}<br />

code snippet frmSerial.cs<br />

The Serialize() method of the XmlSerializer class actually performs the serialization, <strong>and</strong> it has nine<br />

overloads. One of the parameters required is a stream to write the data to. It can be a Stream, TextWriter,<br />

or an XmlWriter parameter. In the example, you create a TextWriter-based object, tr. The next thing to<br />

do is to create the XmlSerializer-based object, sr. The XmlSerializer needs to know type information<br />

for the object that it is serializing, so you use the typeof keyword with the type that is to be serialized.<br />

After the sr object is created, you call the Serialize() method, passing in the tr (Stream-based object)<br />

<strong>and</strong> the object that you want serialized, in this case pd. Be sure to close the stream when you are finished<br />

with it:<br />

//new TextWriter <strong>and</strong> XmlSerializer<br />

TextWriter tr = new StreamWriter("serialprod.xml");<br />

XmlSerializer sr = new XmlSerializer(typeof(Product));<br />

//serialize object<br />

sr.Serialize(tr, pd);<br />

tr.Close();<br />

webBrowser1.Navigate(AppDomain.CurrentDomain.BaseDirectory + "serialprod.xml");<br />

code snippet frmSerial.cs<br />

Next is the Product class, the class to be serialized. The only differences between this <strong>and</strong> any other<br />

class that you may write are the <strong>C#</strong> attributes that have been added. The XmlRootAttribute <strong>and</strong><br />

XmlElementAttribute classes in the attributes inherit from the System.Attribute class. Don’t confuse<br />

these attributes with the attributes in an XML document. A <strong>C#</strong> attribute is simply some declarative<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!