Notice that the root element of the XML document is now <MemberInformation>. Also, <MemberAddress> has now been changed to <Address>, and the <Country> and <Postal> elements are now represented as attributes. Finally, the <City> element is always persisted regardless of whether or not it has been assigned a value.

Here are the uses of each attribute:

[XmlRoot('MemberInformation',

 Namespace = 'http://www.learn2develop.net',

 IsNullable = true)]

public class Member {

 ...

Sets the root element name of the XML document to MemberInformation (default element name is Member, which follows the class name), with a specific namespace. The IsNullable attribute indicates if empty elements must be displayed.

//---specify the element name to be MemberName---

[XmlElement('MemberName')]

public MemberName Name;

Specifies that the element name MemberName be used in place of the current variable name (as defined in the class as Name).

//---specify the sub-element(s) of Addresses to be Address---

[XmlArrayItem('Address')]

public MemberAddress[] Addresses;

Specifies that the following variable is repeating (an array) and that each repeating element be named as Address.

//---empty element if city is not specified---

[XmlElement(IsNullable = true)]

public string City;

Indicates that the document must include the City element even if it is empty.

//---specify country and postal as attribute---

[XmlAttributeAttribute()]

public string Country;

[XmlAttributeAttribute()]

public string Postal;

Indicates that the Country and Postal property be represented as an attribute.

XML Serialization Needs a Default Constructor

There is one more thing that you need to note when doing XML serialization. If your class has a constructor (as in the following example), you also need a default constructor:

[XmlRoot('MemberInformation',

 Namespace = 'http://www.learn2develop.net',

 IsNullable = true)]

public class Member {

 private int age;

 public Member(MemberName Name) {

  this.Name = Name;

 }

 //---specify the element name to be MemberName---

 [XmlElement('MemberName')]

 public MemberName Name;

 ...

This example results in an error when you try to perform XML serialization on it. To solve the problem, simply add a default constructor to your class definition:

[XmlRoot('MemberInformation',

 Namespace = 'http://www.learn2develop.net',

 IsNullable = true)]

public class Member {

 private int age;

 public Member() { }

 public Member(MemberName Name) {

  this.Name = Name;

 }

 ...

Uses of XML Serialization

XML serialization can help you to preserve the state of your object (just like the binary serialization that you saw in previous section) and makes transportation easy. More significantly, you can use XML serialization to manage configuration files. You can define a class to store configuration information and use XML serialization to persist it on file. By doing so, you have the flexibility to modify the configuration information easily because the information is now represented in XML; at the same time, you can programmatically manipulate the configuration information by accessing the object's properties and methods.

Summary

In this chapter, you explored the basics of files and streams and how to use the Stream object to perform a wide variety of tasks, including network communication, cryptography, and compression. In addition, you saw how to preserve the state of objects using XML and binary serialization. In the .NET Framework, the Stream object is extremely versatile and its large number of derived classes is designed to deal with specific tasks such as file I/O, memory I/O, network I/O, and so on.

Chapter 12

Exception Handling

An exception is a situation that occurs when your program encounters an error that it is not expecting during runtime. Examples of exceptions include trying to divide a number by zero, trying to write to a file that is read-only, trying to delete a nonexistent file, and trying to access more members of an array than it actually holds. Exceptions are part and parcel of an application, and as a programmer you need to look out for them by handling the various exceptions that may occur. That means your program must be capable of responding to the exceptions

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату