Serialization 如何在wp7应用程序中序列化CookieContainer?

Serialization 如何在wp7应用程序中序列化CookieContainer?,serialization,windows-phone-7,cookiecontainer,Serialization,Windows Phone 7,Cookiecontainer,我试图序列化cookie以保存它,并在下次启动应用程序时反序列化。但反序列化的结果为空。怎么了 void SaveCookie() { var appStorage = IsolatedStorageFile.GetUserStoreForApplication(); if (this.checkBox_save_passowrd.IsChecked == true) { CookieContainer cc = SEC_Services.Httprequ

我试图序列化cookie以保存它,并在下次启动应用程序时反序列化。但反序列化的结果为空。怎么了

void SaveCookie() {
    var appStorage = IsolatedStorageFile.GetUserStoreForApplication();
    if (this.checkBox_save_passowrd.IsChecked == true)
    {
        CookieContainer cc = SEC_Services.Httprequest.cookie;
        string fileName = "usercookie.xml";
        using (var file = appStorage.OpenFile(fileName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
        {
            using (var writer = new StreamWriter(file))
            {
                System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(CookieContainer));
                xs.Serialize(writer, cc);
                writer.Close();
            }
        }
    }
    else {
        if (appStorage.FileExists("usercookie.xml"))
        {
            appStorage.DeleteFile("usercookie.xml");
        }
    }
}

void ReadCookie() {
    var appStorage = IsolatedStorageFile.GetUserStoreForApplication();
    if (appStorage.FileExists("usercookie.xml"))
    {
        using (System.IO.StreamReader reader = new StreamReader(appStorage.OpenFile("usercookie.xml", FileMode.Open)))
        {
            System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(CookieContainer));
            CookieContainer obj = (CookieContainer)xs.Deserialize(reader);
            reader.Close();
            SEC_Services.Httprequest.cookie = obj;
            if (obj.Count != 0) {
                NavigationService.Navigate(new Uri("/PanoramaPage.xaml", UriKind.Relative));
            }
        }
    }
}
我也发现这很简单 显示CookieContainer可以序列化。但wp7库中没有
SoapFormatter

IsolatedStorageSettings.ApplicationSettings["index"] = yourcookie;
所以你不需要序列化它

我正在一个项目中使用它,因为问题是“如何序列化CookieContainer”,而公认的答案并不能真正回答这个问题。以下是序列化的实现方法:

写入磁盘:

public static void WriteCookiesToDisk(string file, CookieContainer cookieJar)
{
    using(Stream stream = File.Create(file))
    {
        try {
            Console.Out.Write("Writing cookies to disk... ");
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(stream, cookieJar);
            Console.Out.WriteLine("Done.");
        } catch(Exception e) { 
            Console.Out.WriteLine("Problem writing cookies to disk: " + e.GetType()); 
        }
    }
}
从磁盘读取:

public static CookieContainer ReadCookiesFromDisk(string file)
{

        try {
            using(Stream stream = File.Open(file, FileMode.Open))
            {
                Console.Out.Write("Reading cookies from disk... ");
                BinaryFormatter formatter = new BinaryFormatter();
                Console.Out.WriteLine("Done.");
                return (CookieContainer)formatter.Deserialize(stream);
            }
        } catch(Exception e) { 
            Console.Out.WriteLine("Problem reading cookies from disk: " + e.GetType()); 
            return new CookieContainer(); 
        }
}

WP7中没有BinaryFormatter,因此这个答案也不适用。我没有意识到这一点!试试这个答案: