从文件读取和写入嵌套字典?

时间:2012-09-12 07:12:16

标签: c# .net windows-phone-7 dictionary

我是Windows手机的新手。我成功地编写并从文件中读取字典。但是我从文件中读出了嵌套的词典。

  1. Main_dictionary
    1. 登录(键),字典(值)`
    2. 验证(Key),字典(Value)
  2. Main_dictionary
  3. 我需要将Common字典下的这些值写入文件,还需要从同一个文件中读取。任何帮助。

    先谢谢

2 个答案:

答案 0 :(得分:0)

您可以按照XmlSerializer中的说明使用How to XML-serialize a dictionary。它引用了http://huseyint.com/2007/12/xml-serializable-generic-dictionary-tipi/中的示例代码(不是英文,但代码很有用)。

答案 1 :(得分:0)

我得到了解决方案...........

public Dictionary FileRead(string Key)         {             字典> FileResponse = new Dictionary>();            Dictionary ReturnDictionary = new Dictionary();            尝试            {                 使用(IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())                {                    using(IsolatedStorageFileStream fileReader = new IsolatedStorageFileStream(DisplayMessage.Storage_Directory,FileMode.Open,FileAccess.ReadWrite,isolatedStorage))                     {                         DataContractSerializer datacontract = new DataContractSerializer(typeof(Dictionary>));                         FileResponse =(Dictionary>)datacontract.ReadObject(fileReader);                         ReturnDictionary = FileResponse [Key];                     }                 }            }            catch(Exception ex)             {             }             return(ReturnDictionary);         }

   public void FileWrite(string Key,Dictionary<string, string> FiletoStore)
    {
       try
        {
           using (IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                Dictionary<string, Dictionary<string, string>> StoredDictionary = new Dictionary<string, Dictionary<string, string>>();
                if (!isolatedStorage.FileExists(DisplayMessage.Storage_Directory))
               {
                   using (IsolatedStorageFileStream IsolatedfileStream = new IsolatedStorageFileStream(DisplayMessage.Storage_Directory, FileMode.OpenOrCreate, isolatedStorage))
                   {
                        DataContractSerializer datacontract = new DataContractSerializer(typeof(Dictionary<string, Dictionary<string, string>>));
                       StoredDictionary.Add(Key, FiletoStore);
                       datacontract.WriteObject(IsolatedfileStream, StoredDictionary);
                      IsolatedfileStream.Close();
                    }
                }
               else
                {
                    using (IsolatedStorageFileStream IsolatedfileStream = new IsolatedStorageFileStream(DisplayMessage.Storage_Directory, FileMode.Open, isolatedStorage))
                    {
                        DataContractSerializer datacontract = new DataContractSerializer(typeof(Dictionary<string, Dictionary<string, string>>));
                       StoredDictionary.Add(Key, FiletoStore);
                       datacontract.WriteObject(IsolatedfileStream, StoredDictionary);
                      IsolatedfileStream.Close();
                   }
              }
           }
       }
       catch (Exception ex)
      {
       }
    }