我有一个字典,当我向它添加多个值时,之前输入的项目会采用添加的项目的值。我正在使用.Net 3.5 这是代码:
public static Dictionary<string, Neighborhoods> Families()
{
if (File.Exists(calculatePath() + "Family.txt")){}
else {File.Create(calculatePath() + "Family.txt").Close();}
string[] inp = File.ReadAllLines(calculatePath() + "Family.txt");
Neighborhoods temp = new Neighborhoods();
Dictionary<string, Neighborhoods> All_Families = new Dictionary<string, Neighborhoods>();
string currentphase = null;
foreach (string s in inp)
{
switch (s)
{
case "!<Start Family>!": temp = new Neighborhoods();
break;
case "<Family Name>": currentphase = "<Family Name>";
break;
case "<End Family Name>": currentphase = null;
break;
case "<Neighbour Enabled>True": temp.Neighbourhood_Enabled1 = true;
currentphase = "<Neighbour Enabled>True";
break;
case "<Neighbour Enabled>False": temp.Neighbourhood_Enabled1 = false;
temp.Neighbourhood_Input1 = null;
break;
case "<University Enabled>True": temp.University_Enabled1 = true;
currentphase = "<University Enabled>True";
break;
case "<University Enabled>False": temp.University_Enabled1 = false;
temp.University_Input1 = null;
currentphase = null;
break;
case "<Downtown Enabled>True": temp.Downtown_Enabled1 = true;
currentphase = "<Downtown Enabled>True";
break;
case "<Downtown Enabled>False": temp.Downtown_Enabled1 = false;
temp.Downtown_Input1 = null;
currentphase = null;
break;
case "!<End Family>!": All_Families.Add(temp.Name, temp);
break;
default: if (currentphase == "<Family Name>") temp.Name = s;
if (currentphase == "<Neighbour Enabled>True") temp.Neighbourhood_Input1 = s;
if (currentphase == "<University Enabled>True") temp.University_Input1 = s;
if (currentphase == "<Downtown Enabled>True") temp.Downtown_Input1 = s;
break;
}
}
return All_Families;
}
如何做到这一点,以便在添加新键和值时,旧键保持其原始值
样本数据:
!<Start Family>!
Family Name>
qwe
<End Family Name>
<Neighbour Enabled>True
qwe
<University Enabled>True
we
<Downtown Enabled>True
qwe
!<End Family>!
!<Start Family>!
<Family Name>
123
<End Family Name>
<Neighbour Enabled>True
123
<University Enabled>True
123
<Downtown Enabled>True
123
!<End Family>!
这是 nieghbourhoods 类供参考。我会尝试 xml 方法,但它不会很快完成,我还在学习这些东西。
class Neighborhoods
{
public Neighborhoods()
{
name = "";
Neighbourhood_Enabled = false;
Neighbourhood_Input = "";
University_Enabled = false;
University_Input = "";
Downtown_Enabled = false;
Downtown_Input = "";
}
static string name;
public string Name
{
get { return Neighborhoods.name; }
set { Neighborhoods.name = value; }
}
static bool Neighbourhood_Enabled;
public bool Neighbourhood_Enabled1
{
get { return Neighborhoods.Neighbourhood_Enabled; }
set { Neighborhoods.Neighbourhood_Enabled = value; }
}
static string Neighbourhood_Input;
public string Neighbourhood_Input1
{
get { return Neighborhoods.Neighbourhood_Input; }
set { Neighborhoods.Neighbourhood_Input = value; }
}
static bool University_Enabled;
public bool University_Enabled1
{
get { return Neighborhoods.University_Enabled; }
set { Neighborhoods.University_Enabled = value; }
}
static string University_Input;
public string University_Input1
{
get { return Neighborhoods.University_Input; }
set { Neighborhoods.University_Input = value; }
}
static bool Downtown_Enabled;
public bool Downtown_Enabled1
{
get { return Neighborhoods.Downtown_Enabled; }
set { Neighborhoods.Downtown_Enabled = value; }
}
static string Downtown_Input;
public string Downtown_Input1
{
get { return Neighborhoods.Downtown_Input; }
set { Neighborhoods.Downtown_Input = value; }
}
}