如何使用文件中的二进制反序列化反序列化

时间:2014-05-04 03:39:25

标签: c# serialization

public static void SaveRestaurantList(List<Restaurant> restaurantList)
    {           
        FileStream fs = new FileStream("Restaurant.txt", FileMode.Create, FileAccess.Write);
        BinaryFormatter bf = new BinaryFormatter();

        for (int i = 0; i < restaurantList.Count; i++)
        {
            Restaurant r = new Restaurant();
            r = (Restaurant)restaurantList[i];
            bf.Serialize(fs, r);
            fs.Flush();               
        }
        fs.Close();
        Console.WriteLine("\n\n\t\t File Get Serialized.., \n\t\t Close the Promt and Check in Application Debug Folder..!!");
     }

我将Serailze的通用列表存入“Restaurant.txt”文件。

现在我想反序列化并将其返回到通用列表中,我已经尝试过了 但它没有工作,它给出错误“无效的演员表达”。

任何人都可以帮忙解决这个问题。

2 个答案:

答案 0 :(得分:1)

您应该序列化完整列表本身。

using (Stream stream = File.Open("data.bin", FileMode.Create))
{
    BinaryFormatter bin = new BinaryFormatter();
    bin.Serialize(stream, restaurantList);
}

您稍后可以像这样反序列化完整列表

using (Stream stream = File.Open("data.bin", FileMode.Open))
{
    BinaryFormatter bin = new BinaryFormatter();
    var restaurantList=(List<Restaurant>)bin.Deserialize(stream);
}

答案 1 :(得分:0)

Kapadni,

我将对象的列表/ BindingList存储在.xml文件中,可能在函数/代码下面将帮助您序列化和反序列化对象并从.xml文件中存储/检索

BindingList<IntradayData> objIntradayDataList;
        SerializeObject(objIntradayDataList, filepath);
        objIntradayDataList = DeSerializeObject<BindingList<IntradayData>>(filepath);

 public void SerializeObject<T>(T serializableObject, string fileName)
    {
        if (serializableObject == null) { return; }

        try
        {
            XmlDocument xmlDocument = new XmlDocument();
            XmlSerializer serializer = new XmlSerializer(serializableObject.GetType());
            using (MemoryStream stream = new MemoryStream())
            {
                serializer.Serialize(stream, serializableObject);
                stream.Position = 0;
                xmlDocument.Load(stream);
                xmlDocument.Save(fileName);
                stream.Close();
            }
        }
        catch (Exception ex)
        {
            //Log exception here
            log.Error("SerializeObject ", ex);

        }
    }

    public T DeSerializeObject<T>(string fileName)
    {
        if (string.IsNullOrEmpty(fileName)) { return default(T); }

        T objectOut = default(T);

        try
        {
            string attributeXml = string.Empty;

            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(fileName);
            string xmlString = xmlDocument.OuterXml;

            using (StringReader read = new StringReader(xmlString))
            {
                Type outType = typeof(T);

                XmlSerializer serializer = new XmlSerializer(outType);
                using (XmlReader reader = new XmlTextReader(read))
                {
                    objectOut = (T)serializer.Deserialize(reader);
                    reader.Close();
                }

                read.Close();
            }
        }
        catch (Exception ex)
        {
            //Log exception here
            log.Error("DeSerializeObject ", ex);

        }

        return objectOut;
    }