使用ObjectDataSource

时间:2015-07-11 17:30:56

标签: c# asp.net resx objectdatasource

我正在尝试将resx文件与对象数据源连接。但是,当我尝试添加值时,它不会添加,而是替换现有值。有人可以给我一个提示吗?我究竟做错了什么?

        Dictionary<string, string> resources = new Dictionary<string, string>();

        public Dictionary<string, string> SelectIndex()
        {            
            using (ResXResourceReader resxreader = new ResXResourceReader(filename)) {
                foreach (DictionaryEntry entry in resxreader) {                    
                    resources.Add(entry.Key.ToString(), entry.Value.ToString());
                }
            }
            return resources;
        }

        public void AddIndex(Data data)
        {
            using (ResXResourceWriter resx = new ResXResourceWriter(filename)) {                
                resx.AddResource(data.Key, data.Value);                
            }
        }

1 个答案:

答案 0 :(得分:0)

问题是ResXResourceReader内部使用了Dictionary,可以从foreach循环中看到。在Dictionary中,每个Key值都应该是唯一的,如果您使用相同的键添加资源,它将更新这些键的值。

MSDN article about ResXResourceReader

MSDN article about Dictionary

相关问题