你也可以用“LINQ”。
作为一个例子:
程序代码:
IList<Dictionary<string, string>> infos =
File.ReadAllText(@"C:\tmp\test\data.txt")
.Split(new[] { "*********************************" }, StringSplitOptions.RemoveEmptyEntries)
.Select(info => info.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries))
.Select(array => array.Aggregate(new List<string>(), (list, s) => { if (s.Contains("=")) list.Add(s); else list[list.Count - 1] += "\r\n" + s; return list; }))
.Select(list => list.Select(s => { var a = s.Split('='); return new KeyValuePair<string, string>(a[0], a[1]); }).ToDictionary(p => p.Key, p => p.Value))
.ToList();
foreach (var info in infos)
{
Console.WriteLine("Country info :");
foreach (var pair in info)
{
Console.WriteLine("\t{0}: {1}", pair.Key, pair.Value);
}
}