.net中序列化和反序列化

string filePath = "data.xml";//定义文件名
                DataSet ds = new DataSet();
                if (!File.Exists(Server.MapPath(filePath)))
                {
                    ds = 数据;//取数据
                    //序列化
                    BinaryFormatter bin = new BinaryFormatter();
                    StreamWriter dat = new StreamWriter(Server.MapPath(filePath));
                    bin.Serialize(dat.BaseStream, ds);
                    dat.Close();
                }
                else
                {
                    //反序列化
                    BinaryFormatter bin = new BinaryFormatter();
                    StreamReader sr = new StreamReader(Server.MapPath(filePath));
                    ds = (DataSet)bin.Deserialize(sr.BaseStream);
                    sr.Close();
                }

posted on 2007-03-25 20:29  myer  阅读(152)  评论(0编辑  收藏  举报

导航