try {
XmlSerializer s = new XmlSerializer(typeof(Member));
s.Serialize(sw, mem);
} catch (Exception ex) {
Console.WriteLine(ex.ToString());
} finally {
sw.Close();
}
}
For XML serialization, you need to import the System.Xml.Serialization
namespace.
In the XMLSerialize()
function, you first create a new StreamWriter
object so that you can save the serialized XML string to a file. The Serialize()
method from the XMLSerializer
class serializes the Member object into an XML string, which is then written to file by using the StreamWriter
class.
To test the XMLSerialize()
function, assume that you have the following object declarations:
static void Main(string[] args) {
MemberAddress address1 = new MemberAddress() {
Line1 = "One Way Street",
Line2 = "Infinite Loop",
Country = "SINGAPORE",
Postal = "456123"
};
MemberAddress address2 = new MemberAddress() {
Line1 = "Two Way Street",
Country = "SINGAPORE",
Postal = "456123"
};
Member m1 = new Member() {
Name = new MemberName() {
FirstName = "Wei-Meng", LastName = "Lee"
},
DOB = Convert.ToDateTime(@"5/1/1972"),
Addresses = new MemberAddress[] { address1, address2 }
};
}
To serialize the Member object, invoke the XMLSerialize()
method like this:
static void Main(string[] args) {
MemberAddress address1 = new MemberAddress() {
Line1 = "One Way Street",
Line2 = "Infinite Loop",
Country = "SINGAPORE",
Postal = "456123"
};
MemberAddress address2 = new MemberAddress() {
Line1= "Two Way Street",
Country = "SINGAPORE",
Postal = "456123"
};
Member m1 = new Member() {
Name = new MemberName() {
FirstName = "Wei-Meng",
LastName = "Lee"
},
DOB = Convert.ToDateTime(@"5/1/1972"),
Addresses = new MemberAddress[] { address1, address2 }
};
XMLSerialize(m1);
}
Figure 11-15 shows the XML document generated by the XMLSerialize()
function.
Figure 11-15
As you can see, the object is serialized into an XML document with a format corresponding to the structure of the object. Here are some important points to note:
□ The City
information is not persisted in the XML document (nor as the Line2
in the second Address
element) because it was not assigned in the objects. You will see later how to persist empty elements, even though a value is not assigned.
□ All read/write properties in the object are persisted in the XML document, except the read-only currentAge
property in the Member class.
□ Only public variables are persisted; private variables are not persisted in XML serialization.
□ The default name for each element in the XML document is drawn from the variable (or class) name. In most cases this is desirable, but sometimes the element names might not be obvious.
Deserializing the Class
To deserialize the XML document, simply use the Deserialize()
method from the XMLSerializer class. Define the XMLDeserialize() function as follows:
static void Main(string[] args) {
//...
}
//========XML Serialization=========
static Member XMLDeserialize(string xmlFile) {
Member obj;
XmlReader xr = XmlReader.Create(xmlFile);
try {
XmlSerializer s = new XmlSerializer(typeof(Member));
obj = (Member)s.Deserialize(xr);
} catch (Exception ex) {
Console.WriteLine(ex.ToString());
obj = null;
} finally {
xr.Close();
}
return obj;
}
Here, you can use the XmlReader
class's Create()
method to open an XML file for reading.
The XmlReader
class is used to read the data from the XML file. The deserialized object is then returned to the calling function.
Remember to import the System.Xml
namespace for the XmlReader
class.
To test the XMLDeserialize()
function, call it directly after an object has been serialized, like this:
static void Main(string[] args) {
MemberAddress address1 = new MemberAddress() {
Line1 = "One Way Street",
Line2 = "Infinite Loop",
Country = "SINGAPORE",
Postal = "456123"
};
MemberAddress address2 = new MemberAddress() {
Line1 = "Two Way Street",
Country = "SINGAPORE",
Postal = "456123"
};
Member m1 = new Member() {
Name = new MemberName() {
FirstName = "Wei-Meng",
LastName = "Lee"
},
DOB = Convert.ToDateTime(@"5/1/1972"),
Addresses = new MemberAddress[] { address1, address2 }
};
XMLSerialize(m1);
Member m2 = XMLDeserialize(@"c:\Members.xml");
Console.WriteLine("{0}, {1}", m2.Name.FirstName, m2.Name.LastName);
Console.WriteLine("{0}", m2.currentAge);
foreach (MemberAddress a in m2.Addresses) {
Console.WriteLine("{0}", a.Line1);
Console.WriteLine("{0}", a.Line2);
Console.WriteLine("{0}", a.Country);
Console.WriteLine("{0}", a.Postal);
Console.WriteLine();
}
Console.ReadLine();
}
The output of these statements is shown in Figure 11-16.
Figure 11-16
Customizing the Serialization Process
Despite the fairly automated task performed by the XMLSerializer object, you can customize the way the XML document is generated. Here's an example of how you can modify classes with a few attributes:
[XmlRoot("MemberInformation",
Namespace = "http://www.learn2develop.net",
IsNullable = true)]
public class Member {
Читать дальше