我在做一个学生信息管理系统,然后,在退出时准备把信息存入文件中,如何使用Serialize()和 DeSerialize()方法。main函数我已经有 了,那Serialize()和 DeSerialize()怎么初始化啊
解决方案 »
- win7让人纠结啊.看不到抛出异常信息.只有友好提示.让你关闭还是调试.
- .NET 登录Gmail 报异常
- 请问如何判断自己设计的软件对运行环境的要求?
- 正则表达式去除 sql里面 from 前面的逗号,高手们来帮帮忙
- 请问如何给一个DataGrid添加自增行标
- 请问C#showDialog一个窗体时,如何转移焦点呢
- 请问各位nhibernate怎么用在c/s中?
- 用.NET做WinForm安装包的问题(关于得到安装路径)
- 国内第一款专业的矢量动画制作软件即将发布,基于SVG,采用C#开发
- 这段代码是什么意思?
- 建立的asp.net程序运行时总是提示"导航已取消"?
- 如何让textbox的内容自动滚动显示
using System.IO;
using System.Xml.Serialization;// This is the class that will be serialized.
public class OrderedItem
{
public string ItemName;
public string Description;
public decimal UnitPrice;
public int Quantity;
public decimal LineTotal; // A custom method used to calculate price per item.
public void Calculate()
{
LineTotal = UnitPrice * Quantity;
}
}public class Test{
public static void Main(string[] args)
{
Test t = new Test();
// Write a purchase order.
t.SerializeObject("simple.xml");
} private void SerializeObject(string filename)
{
Console.WriteLine("Writing With Stream"); XmlSerializer serializer =
new XmlSerializer(typeof(OrderedItem));
OrderedItem i = new OrderedItem();
i.ItemName = "Widget";
i.Description = "Regular Widget";
i.Quantity = 10;
i.UnitPrice = (decimal) 2.30;
i.Calculate(); // Create a FileStream to write with.
Stream writer = new FileStream(filename, FileMode.Create);
// Serialize the object, and close the TextWriter
serializer.Serialize(writer, i);
writer.Close();
}
}
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;// This is the class that will be deserialized.
public class OrderedItem
{
public string ItemName;
public string Description;
public decimal UnitPrice;
public int Quantity;
public decimal LineTotal; // A custom method used to calculate price per item.
public void Calculate()
{
LineTotal = UnitPrice * Quantity;
}
}
public class Test
{
public static void Main(string[] args)
{
Test t = new Test();
// Read a purchase order.
t.DeserializeObject("simple.xml");
} private void DeserializeObject(string filename)
{
Console.WriteLine("Reading with XmlReader"); // Create an instance of the XmlSerializer specifying type and namespace.
XmlSerializer serializer = new
XmlSerializer(typeof(OrderedItem)); // A FileStream is needed to read the XML document.
FileStream fs = new FileStream(filename, FileMode.Open);
XmlReader reader = XmlReader.Create(fs); // Declare an object variable of the type to be deserialized.
OrderedItem i; // Use the Deserialize method to restore the object's state.
i = (OrderedItem)serializer.Deserialize(reader);
fs.Close(); // Write out the properties of the object.
Console.Write(
i.ItemName + "\t" +
i.Description + "\t" +
i.UnitPrice + "\t" +
i.Quantity + "\t" +
i.LineTotal);
}
}