asp.Netシーケンス化
5947 ワード
、 ( 、 ) ( 、 ) 。
:
1、 。 , , 。
2、 。 , , , , 。
, 、 。 。 , , 。.NET 。 , Serializable( )。 , 。 , , 。
、
1、 。BinaryFormatter using System.Runtime.Serialization.Formatters.Binary;
/// <summary>
///
/// </summary>
static void SerializaleObject()
{
List<Person> peoples = new List<Person>();
peoples.Add(new Person(" ", 18));
peoples.Add(new Person(" ", 19));
FileStream fs = new FileStream("peoples.dat", FileMode.Create);//
BinaryFormatter bf = new BinaryFormatter(); //
bf.Serialize(fs, peoples); //
fs.Close();
Console.WriteLine(" , 。");
Console.ReadLine();
}
/// <summary>
///
/// </summary>
static void DeserializeObject()
{
FileStream fs = new FileStream("peoples.dat", FileMode.Open); //
BinaryFormatter bf = new BinaryFormatter(); //
List<Person> peoples = (List<Person>)bf.Deserialize(fs); //
fs.Close();
peoples.Sort();
foreach (Person person in peoples)
{
Console.WriteLine(person.Name + " " + person.Age + " 。");
}
Console.ReadLine();
}
2、XML , using System.Xml.Serialization;
/// <summary>
/// XML
/// </summary>
static void SerializaleObject1()
{
List<Person> peoples = new List<Person>();
peoples.Add(new Person(" ", 18));
peoples.Add(new Person(" ", 19));
FileStream fs = new FileStream("peoples.xml", FileMode.Create);//
XmlSerializer formatter = new XmlSerializer(typeof(List<Person>)); // XML ,SoapFormatter SOAP
formatter.Serialize(fs, peoples); // XML
fs.Close();
Console.WriteLine(" , 。");
Console.ReadLine();
}
/// <summary>
/// XML
/// </summary>
static void DeserializeObject1()
{
FileStream fs = new FileStream("peoples.dat", FileMode.Open); //
XmlSerializer formatter = new XmlSerializer(typeof(List<Person>)); // XML
List<Person> peoples = (List<Person>)formatter.Deserialize(fs); // XML
fs.Close();
peoples.Sort();
foreach (Person person in peoples)
{
Console.WriteLine(person.Name + " " + person.Age + " 。");
}
Console.ReadLine();
}
3、SOAP using System.Runtime.Serialization.Formatters.Soap;
/// <summary>
/// SOAP
/// </summary>
static void SerializaleObject1()
{
List<Person> peoples = new List<Person>();
peoples.Add(new Person(" ", 18));
peoples.Add(new Person(" ", 19));
FileStream fs = new FileStream("peoples.xml", FileMode.Create);//
SoapFormatter formatter = new SoapFormatter(typeof(List<Person>)); // SOAP ,SoapFormatter SOAP
formatter.Serialize(fs, peoples); // SOAP
fs.Close();
Console.WriteLine(" , 。");
Console.ReadLine();
}
/// <summary>
/// SOAP
/// </summary>
static void DeserializeObject1()
{
FileStream fs = new FileStream("peoples.dat", FileMode.Open); //
SoapFormatter formatter = new SoapFormatter(typeof(List<Person>)); // SOAP
List<Person> peoples = (List<Person>)formatter.Deserialize(fs); // SOAP
fs.Close();
peoples.Sort();
foreach (Person person in peoples)
{
Console.WriteLine(person.Name + " " + person.Age + " 。");
}
Console.ReadLine();
}