1

我有一个.ini.ini文件:

;app ...

    [MSGS]

    title#0 = first title.. 
    message#0 = first message 

    title#1 = second title
    message#1 = second message 

    title#2 = third title
    message#2 = third message  

我正在使用Nini lib 进行解析。我需要把它读到字典里。

我试过这个:

 public Dictionary<string, string> Read()
        {
            try
            {
                Dictionary<string, string> result = new Dictionary<string, string>();
                IConfigSource src = config;
                IConfig values = src.Configs["MSGS"];
                string[] keys = values.GetKeys();

                for (int count = keys.Length / 2,i = 0, j = 1; 
                        i < count; i++, 
                        j = i + 1)
                {
                    string titleKey = keys[i];
                    string messageKey = keys[j];
                    string titleVal = values.Get(titleKey);
                    string messageVal = values.Get(messageKey);
                    result.Add(titleVal, messageVal);

                }
            }
            catch (Exception)
            {

            }
            return null;
        }

输出是:

first title.. : first message
first message : second title
second title : second message

我想:

first title.. : first message
second title : second message
third title : third message

我该怎么做?提前致谢。:)

4

2 回答 2

6
            for (int i = 0; i < keys.Length; i += 2)
            {
                string titleKey = keys[i];
                string messageKey = keys[i+1];
                string titleVal = values.Get(titleKey);
                string messageVal = values.Get(messageKey);
                result.Add(titleVal, messageVal);
            }
于 2011-11-05T18:19:42.160 回答
2

您的循环中有太多变量 -不需要j并且count只会混淆问题:

for (i = 0; i < keys.Length; i += 2)
{
  string titleKey = keys[i];
  string messageKey = keys[i + 1];
  string titleVal = values.Get(titleKey);
  string messageVal = values.Get(messageKey);
  result.Add(titleVal, messageVal);
}
于 2011-11-05T18:17:27.047 回答