Xml
Using System;
Using System.Xml.Serialization;
Using System.Xml;
Using System.IO;
Using System.Text;
Namespace ConsoleApplication4
{
<summary>
Summary description of the CLASS1.
</summary>
Class Customers
{
<summary>
The main entry point for the application.
</summary>
[STAThread]
static void Main (string[] args) {
Customer customer = Customers.getgustomer ();
SerializerCustomer1 (customer);
SerializerCustomer2 (customer);
SerializerCustomer3 (customer);
Console.ReadLine ();
}
public static Customer Getgustomer () {
Customer customer = new Customer ();
Address = new address ();
Address. City = "Beijing";
Address. State = "Fengtai";
Address. Street = "Majiabu in the West";
Customer. address = address;
Customer. Name = "Billchen";
return customer;
}
private static void SerializerCustomer1 (Customer customer) {
XmlSerializer ser = new XmlSerializer (typeof (Customer));
FileStream stream = new FileStream ("Test.xml", FileMode.OpenOrCreate);
Ser. Serialize (stream, customer);
Stream. Close ();
}
private static void SerializerCustomer2 (Customer customer) {
XmlSerializer ser = new XmlSerializer (typeof (Customer));
MemoryStream stream = new MemoryStream (100);
Ser. Serialize (stream, customer);
Stream. Position = 0;
using (StreamReader reader = new StreamReader (stream, Encoding.UTF8)) {
Console.Write (reader. ReadToEnd ());
}
}
private static void SerializerCustomer3 (Customer customer) {
XmlSerializer ser = new XmlSerializer (typeof (Customer));
MemoryStream stream = new MemoryStream (100);
XmlTextWriter writer = new XmlTextWriter (stream, Encoding.UTF8);
Writer. formatting = formatting.indented;//Indent
Ser. Serialize (writer, customer);
Stream. Position = 0;
using (StreamReader reader = new StreamReader (stream, Encoding.UTF8)) {
String line;
while (line = reader. ReadLine ())!= null) {
Console.WriteLine (line);
}
}
Writer. Close ();
}
}
[Serializable]
public class Address {
Public address () {}
public String Street {
get {return street;}
set {street = value;}
}private String Street;
public string City {
get {return city;}
set {city = value;}
}private string City;
public string State {
get {return state;}
set {state = value;}
}private string State;
}
[Serializable]
public class Customer {
Public Customer () {}
public string Name {
get {return name;}
set {name = value;}
}private string name;
Public Address Address {
get {return address;}
set {address = value;}
}private address;
}
}