繁体   English   中英

C#序列化,对象数组的反序列化

[英]C# Serialization, Deserialization of an array of objects

我有这个代码。

        const int maxbooks = 5;
        Book[] booklist = new Book[maxbooks];

        FileStream fs = File.Open(@"books.txt", FileMode.Open, FileAccess.Read);
        SoapFormatter sf = new SoapFormatter();

        try
        {

           // something here, deserializing file and assigning to the array

        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        finally
        {
            fs.Close();
        }

我已经弄清楚(或者至少我想出了)如何在一个单独的程序中序列化原始对象数组。 我现在正在寻找反序列化的信息,并使用反序列化的数据创建一个新的数组。 仅供参考,这是我序列化原始数组的另一个文件

        Book firstbook = new Book("Jimbob", "Jimbob book", "first edition", "Jimbob publishing", "1991");
        Book secondbook = new Book("Greg", "Greg book", "third edition", "Unholy publishing", "2010");
        Book thirdbook = new Book("Pingu", "Pingu book", "tenth edition", "Antarctic publishing", "1897");
        Book fourthbook = new Book("Patrick", "Patrick book", "seventh edition", "underwater publishing", "1991");
        Book fifthbook = new Book("Sally", "Sally book", "first edition", "Wowpublishing", "2015");


        const int maxbooks = 5;

        Book[] booklist = new Book[maxbooks];
        booklist[0] = firstbook;
        booklist[1] = secondbook;
        booklist[2] = thirdbook;
        booklist[3] = fourthbook;
        booklist[4] = fifthbook;


        // writing to a file

        FileStream fs = File.Open(@"books.txt", FileMode.Create, FileAccess.Write);
        SoapFormatter sf = new SoapFormatter();

        int bookindex = 0;

        try
        {
            while (bookindex < maxbooks)
            {
                sf.Serialize(fs, booklist[bookindex]);
                bookindex += 1;
            }
        }

        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
       finally
        {
            fs.Close();
        }

目前,使用SOAP序列化。 任何帮助将不胜感激。

序列化数组本身而不是每个项目。 使用序列化程序例程,您将创建许多无效的序列化块,这些序列化块一起附加在文件中。

使用XMLSerializer

例如,像这样序列化:

    private async Task SaveSettings(Settings settings)
    {

        var folder = Windows.Storage.ApplicationData.Current.LocalFolder;
        var options = Windows.Storage.CreationCollisionOption.ReplaceExisting;

        var file = await folder.CreateFileAsync("Settings.XML", options);
        try
        {


            XmlSerializer SerializerObj = new XmlSerializer(typeof(Settings));

            SerializerObj.Serialize(await file.OpenStreamForWriteAsync(), settings);
        }
        catch
        {
            // handle any kind of exceptions
        }

    }

像这样反序列化:

    private async Task<Settings> LoadSettings()
    {
        Settings settings = new Settings(); 
        var folder = Windows.Storage.ApplicationData.Current.LocalFolder;
        try
        {
            var file = await folder.GetFileAsync("Settings.XML");
            XmlSerializer SerializerObj = new XmlSerializer(typeof(Settings));
            settings = SerializerObj.Deserialize(await file.OpenStreamForReadAsync()) as Settings;
        }
        catch (Exception ex)
        {
            // handle any kind of exceptions
        }
        return settings;
    }

本示例序列化一个称为“设置”的对象。 您可以更改它以序列化对象数组。

这是来自Windows 8应用程序,因此您可能需要对其稍加调整。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM