0

我有一个字典,当我向它添加多个值时,之前输入的项目会采用添加的项目的值。我正在使用.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; }
    }
}
4

3 回答 3

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);
        }

它工作得很好,虽然我没有在我的代码中添加任何错误处理以保持简短。

于 2008-11-09T20:14:14.547 回答
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 代码。这些字段意味着与每个实例相关,而不仅仅是类型本身 - 所以它们不应该是静态的。

为了证明这与解析器无关,试试这个:

Neighborhoods first = new Neighborhoods();
Neighborhoods second = new Neighborhoods();

first.Name = "First";
Console.WriteLine(second.Name);

你会看到它打印出“First”——这显然不是你想要的!

不幸的是,我没有关于“静态”意味着什么的好页面,但我建议您在您拥有的任何 C# 书籍中查找它。

于 2008-11-09T20:38:25.777 回答
0

就像 Jon Skeet 指出的那样,你不能像那样使用静态。

用私有替换静态,一切都应该很好。

于 2008-11-09T20:44:32.143 回答