XML serialization and deserialization

Source: Internet
Author: User
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;
}
}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.