序列化和反序列化

序列化和反序列化

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Xml;

using System.Xml.Serialization;

using System.IO;

 

namespace ConsoleApplication8

{


public
class
XMLWrite

{

 


static
void Main(string[] args)

{

WriteXML();

}

 


public
class
Book

{


public
String title;

}

 


public
static
void WriteXML()

{


Book overview = new
Book();

overview.title = “Serialization Overview”;

 


XmlSerializer writer = new
XmlSerializer(typeof(Book));


string path = @”C:\Users\xudajun\Desktop\test” + @”\SerializationOverview.xml”;


FileStream file = File.Create(path);

writer.Serialize(file, overview);

file.Close();

 


XmlSerializer reader = new
XmlSerializer(typeof(Book));


StreamReader f =new
StreamReader(path);


Book book1 = (Book)reader.Deserialize(f);

f.Close();

 


Console.WriteLine(book1.title);

}

}

}