0

我是 Windows 手机的新手。我成功地从文件中写入和读取字典。但是我一直坚持从文件中读取嵌套字典。

  1. Main_dictionary
    1. 登录(键),字典(值)`
    2. 验证(键),字典(值)
  2. Main_dictionary

我需要将公共字典下的这些值写入文件,还需要从同一个文件中读取。任何帮助。

提前致谢

4

2 回答 2

0

您可以按照如何对字典进行 XML 序列化中所述使用XmlSerializer它引用了http://huseyint.com/2007/12/xml-serializable-generic-dictionary-tipi/中的示例代码(不是英文,但代码很有帮助)。

于 2012-09-12T12:11:08.113 回答
0

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

public Dictionary FileRead(string Key) { Dictionary > FileResponse = new Dictionary>(); Dictionary ReturnDictionary = new Dictionary(); 尝试 { 使用 (IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) { 使用 (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)
      {
       }
    }
于 2012-09-20T05:43:06.920 回答