我有一个字典,当我向其添加多个值时,之前输入的项目会添加项目的值。我使用的是.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; }
}
}
答案 0 :(得分:2)
从它的外观来看,你想要解析一个文件,然后将它加载到Dictionary集合中。
一些批评......使用XML作为一个,并摆脱疯狂的解析器:
<?xml version="1.0" encoding="utf-8"?>
<families>
<family>
<name>Smith</name>
<neighborhood>true</neighborhood>
<university>false</university>
<downtown>false</downtown>
</family>
<family>
<name>Jones</name>
<neighborhood>false</neighborhood>
<university>true</university>
<downtown>false</downtown>
</family>
</families>
现在,我们可以使用内置的System.XML命名空间来解析这个问题。
例如,我只是将您的代码重写为:
Dictionary<String, Neighborhood> families = new Dictionary<string, Neighborhood>();
XmlDocument doc = new XmlDocument();
doc.Load("family.xml");
foreach (XmlNode familyNode in doc.SelectNodes("//family"))
{
Neighborhood n = new Neighborhood();
n.Name = familyNode.SelectSingleNode("name").InnerText;
n.InNeighborhood = Boolean.Parse(familyNode.SelectSingleNode("neighborhood").InnerText);
n.InDowntown = Boolean.Parse(familyNode.SelectSingleNode("downtown").InnerText);
n.InUniversity = Boolean.Parse(familyNode.SelectSingleNode("university").InnerText);
families.Add(n.Name,n);
}
它工作正常,但我没有在代码中添加任何错误处理以保持简洁。
答案 1 :(得分:2)
根据您提供的示例数据和您提供的代码,使用Neighborhoods
这样的类可以正常工作:
public class Neighborhoods
{
public string Name { get; set; }
public string Neighbourhood_Input1 { get; set; }
public string University_Input1 { get; set; }
public string Downtown_Input1 { get; set; }
public bool Neighbourhood_Enabled1 { get; set; }
public bool University_Enabled1 { get; set; }
public bool Downtown_Enabled1 { get; set; }
}
我的测试是运行此代码:
static void Main()
{
var families = Families();
foreach (var family in x.Values)
{
Console.WriteLine(y.Name);
}
}
打印出“qwe”和“123” - 表示涉及两个不同的对象。
但是,我们还没有看到真正的Neighborhoods
课程。我不认为它使用静态字段(但仍然是实例属性)是吗?这肯定会解释你所看到的行为。
要显示这与解析器无关,请尝试:
Neighborhoods first = new Neighborhoods();
Neighborhoods second = new Neighborhoods();
first.Name = "First";
Console.WriteLine(second.Name);
你会看到它打印出“First” - 这显然不是你想要的!
不幸的是,我没有关于“静态”意味着什么的好页面,但我建议你在任何C#书籍中查找它。
答案 2 :(得分:0)
就像Jon Skeet所说,你不能像那样使用静态。
用private替换static,一切都应该好。