我是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