我是 Windows 手机的新手。我成功地从文件中写入和读取字典。但是我一直坚持从文件中读取嵌套字典。
- Main_dictionary
- 登录(键),字典(值)`
- 验证(键),字典(值)
- Main_dictionary
我需要将公共字典下的这些值写入文件,还需要从同一个文件中读取。任何帮助。
提前致谢
我是 Windows 手机的新手。我成功地从文件中写入和读取字典。但是我一直坚持从文件中读取嵌套字典。
我需要将公共字典下的这些值写入文件,还需要从同一个文件中读取。任何帮助。
提前致谢
您可以按照如何对字典进行 XML 序列化中所述使用XmlSerializer。它引用了http://huseyint.com/2007/12/xml-serializable-generic-dictionary-tipi/中的示例代码(不是英文,但代码很有帮助)。
我得到了解决方案............
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)
{
}
}