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:
□
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---
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).
□
public MemberAddress[] Addresses;
Specifies that the following variable is repeating (an array) and that each repeating element be named as Address
.
□
public string City;
Indicates that the document must include the City element even if it is empty.
□
//---specify country and postal as attribute---
public string Country;
public string Postal;
Indicates that the Country
and Postal
property be represented as an attribute.
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;
//---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;
}
...
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