我是Windows手機的新手。我成功地從一個文件中書寫和閱讀字典。但是我從一個文件中讀取嵌套字典時遇到了困難。從文件中讀取和寫入嵌套字典?
- Main_dictionary
- 登錄(密鑰),字典(值)`
- 驗證(密鑰),字典(值)
- Main_dictionary
我需要寫這些值在Common字典下的一個文件中,並且還需要從相同的文件中讀取。任何幫助。
由於提前
我是Windows手機的新手。我成功地從一個文件中書寫和閱讀字典。但是我從一個文件中讀取嵌套字典時遇到了困難。從文件中讀取和寫入嵌套字典?
我需要寫這些值在Common字典下的一個文件中,並且還需要從相同的文件中讀取。任何幫助。
由於提前
可以使用爲How to XML-serialize a dictionary描述。它參考http://huseyint.com/2007/12/xml-serializable-generic-dictionary-tipi/中的示例代碼(不是英文,但代碼很有用)。
我得到的溶液...........
公共字典FILEREAD(字符串密鑰) { 字典> FileResponse =新詞典>(); Dictionary ReturnDictionary = new Dictionary(); 嘗試 { 使用(IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()){ 使用 (IsolatedStorageFileStream的FileReader =新IsolatedStorageFileStream(DisplayMessage.Storage_Directory,FileMode.Open,FileAccess.ReadWrite,isolatedStorage)) { 的DataContractSerializer datacontract =新的DataContractSerializer (typeof運算(詞典>)); FileResponse =(Dictionary>)datacontract.ReadObject(fileReader); ReturnDictionary = FileResponse [Key]; } } } 趕上(異常前) { } 回報(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)
{
}
}
請正確格式化文件內容。 – abatishchev