c#反序列化不起作用?

时间:2011-05-09 00:10:31

标签: c# serialization deserialization

我有一个测试控制台应用程序,我正在尝试使用serializaiton和反序列化来将数据存储到xml文件中。 serialize方法正常工作,但反序列化似乎不起作用。它不会产生任何错误,但它不会从.xml文件加载我的数据。谁知道为什么?

class Program
{
    static void Main(string[] args)
    {
        ArrayList siteList = new ArrayList();
        DeserializeArray();
        siteList.Add("Test 1");
        siteList.Add("Test 2");
        foreach (var item in siteList)
        {
            Console.WriteLine(item);
        }
        SerializeArray(siteList);
        if (siteList.Contains("Test 2"))
        {
            Console.WriteLine("Test 2 exists!");
            Console.Read();
        }
    }

    public static void SerializeArray(ArrayList siteList)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(ArrayList));
        TextWriter textWriter = new StreamWriter("SiteList.xml");
        serializer.Serialize(textWriter, siteList);
        textWriter.Close();
    }

    static ArrayList DeserializeArray()
    {
        XmlSerializer deserializer = new XmlSerializer(typeof(ArrayList));
        TextReader textReader = new StreamReader("SiteList.xml");
        ArrayList siteList;
        siteList = (ArrayList)deserializer.Deserialize(textReader);
        textReader.Close();

        return siteList;
    }
}

}

2 个答案:

答案 0 :(得分:1)

它工作正常(已测试),Main()中的逻辑已关闭 - 您必须在反序列化之前先进行序列化,请尝试以下操作:

static void Main(string[] args)
{

    ArrayList siteList = new ArrayList();

    siteList.Add("Test 1");
    siteList.Add("Test 2");
    foreach (var item in siteList)
    {
        Console.WriteLine(item);
    }
    SerializeArray(siteList);
    siteList = DeserializeArray();
    if (siteList.Contains("Test 2"))
    {
        Console.WriteLine("Test 2 exists!");
        Console.Read();
    }
}

答案 1 :(得分:1)

您的代码在这里工作正常。我假设您从上次运行中获得了SiteList.xml,因为您首先进行反序列化。为了清楚起见,我添加了Console.WriteLine来转储反序列化的数据。

class Program
{
    static void Main(string[] args)
    {
        ArrayList siteList = new ArrayList();
        ArrayList deserealizedArray = DeserializeArray();
        foreach (var item in deserealizedArray)
        {
            Console.WriteLine(item);
        }
        Console.WriteLine("---");
        siteList.Add("Test 1");
        siteList.Add("Test 2");
        foreach (var item in siteList)
        {
            Console.WriteLine(item);
        }
        SerializeArray(siteList);
        if (siteList.Contains("Test 2"))
        {
            Console.WriteLine("Test 2 exists!");
            Console.Read();
        }
    }

    public static void SerializeArray(ArrayList siteList)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(ArrayList));
        TextWriter textWriter = new StreamWriter("SiteList.xml");
        serializer.Serialize(textWriter, siteList);
        textWriter.Close();
    }

    static ArrayList DeserializeArray()
    {
        XmlSerializer deserializer = new XmlSerializer(typeof(ArrayList));
        TextReader textReader = new StreamReader("SiteList.xml");
        ArrayList siteList;
        siteList = (ArrayList)deserializer.Deserialize(textReader);
        textReader.Close();

        return siteList;
    }
}