代码之家  ›  专栏  ›  技术社区  ›  Kanini

一组C语言的字典可能吗?

  •  0
  • Kanini  · 技术社区  · 15 年前

    我可以使用字典对象数组吗?

    我有一个要修改的XML。我要使用的数据结构是-

    Dictionary<element, Dictionary<attr, value>>
    

    元素-是我将要修改的元素 attr-我要更新其值的属性 值-我要更新的值

    <Parents>
        <Parent id="ParentA" description="New">
            <Children>
                <Child name="ChildA" />
            </Children>
        </Parent>
    </Parents>
    

    我想通过下面的

    Dictionary<string, string> dict_Attributes1=new Dictionary<string, string>();
    Dictionary<string, string> dict_Attributes2=new Dictionary<string, string>();
    dict_Attributes1.Add("id", "ParentB");
    dict_Attributes1.Add("description", "Old");
    dict_Attributes2.Add("name", "ChildB");
    Dictionary<string, Dictionary<string, string>> dict_Elements = new Dictionary<string, Dictionary<string, string>>();
    dict_Elements.Add(".", dict_Attributes1);//To update the Element
    dict_Elements.Add("Children/Child", dict_Attributes2);//To update the Children's Attributes
    

    假设我已经确定要更新ID为parentA的父级。

    现在,我在这里创建dict_attributes1、dict_attributes2等,有没有一种方法可以将它们存储在字典对象的(动态,编译时大小未知)数组中?

    或者,有没有更好的方法- 1。是否修改所选Xelement及其子元素的属性?

    编辑

    <?xml version="1.0" encoding="utf-8"?>
    <Configuration>
        <Environments>
            <Environment id="Master" description="MasterSystem">
                <Systems>
                    <DefaultSystem systemName="Master" server="http://localhost" />
                </Systems>
            </Environment>
        </Environments>
    </Configuration>
    

    现在,当用户更改ID和描述时,我想用新值更新这个XML文件。在更改ID和描述(从用户获取)时,我希望使用相同的ID值更新系统名。

    如果新ID为“开发”,说明为“开发系统”,

    输出XML应该是

    <?xml version="1.0" encoding="utf-8"?>
    <Configuration>
        <Environments>
            <Environment id="Development" description="DeveloperSystem">
                <Systems>
                    <DefaultSystem systemName="Development" server="http://localhost" />
                </Systems>
            </Environment>
        </Environments>
    </Configuration>
    
    5 回复  |  直到 15 年前
        1
  •  7
  •   Justin Niessner    15 年前

    是的,这是可能的。我不知道你需要哪一个,但他们都用 List<T> :

    var list = new List<Dictionary<string, string>>();
    

    或:

    var list = new List<Dictionary<string, Dictionary<string, string>>>();
    
        2
  •  3
  •   Wonko the Sane    15 年前

    虽然你想做的是允许的,但我会亲自使用 LINQ to XML .

    这里有一个轻微的学习曲线,但是如果您至少熟悉LINQ和XML,那么在稍微使用它之后,它将变得有意义。它可能比您所建议的结构更有效,而且肯定更容易适应和维护(由您,尤其是由其他人),因为它们是一种标准化技术。

        3
  •  2
  •   Merlyn Morgan-Graham    15 年前

    有比数组更好的选项,它将按我认为您需要的方式工作:

    var dictionaries = new List<Dictionary<string, string>>();
    

    这是动态可调整大小的,无需将内容重新复制到新数组。与向字典中添加新条目的方式类似,您可以向列表中添加新条目:

    var dict = new Dictionary<string, string> { { "Key1", "Value1" }, };
    dictionaries.Add(dict);
    

    关于你提出的具体问题:

    现在,我在这里创建dict_attributes1、dict_attributes2等,有没有一种方法可以将它们存储在字典对象的(动态,编译时大小未知)数组中?

    是的,您可以创建一个 数组 字典对象的。这与您在代码顶部指定的字典不同,并且不容易调整大小(尽管可以在运行时指定大小,您可以通过创建新数组并复制旧数组的内容来“调整”它的大小)。

    一系列字典如下所示:

    var dictionaries = new Dictionary<string, string>[0];
    

    如果要在运行时指定大小,只需替换 [0] 有一个变量。

        4
  •  1
  •   Andrew Cooper    15 年前

    您可以尝试使用 ArrayList<Dictionary<string,string>>

    ArrayList 行为类似于数组,但可以动态添加到。

    或者,查看中可用的类 System.Xml.Linq

        5
  •  0
  •   jb.    15 年前

    如果这样做的目的是编辑XML文件,那么您就使用了错误的工具。我愿意 强烈地 建议使用LINQ to XML。下面是一个非常简单的例子,说明如何使用LINQ修改XML文件。

    using System.Xml.Linq;
    //starting with the XML you provided:
    /*
    <Parents>
        <Parent id="ParentA" description="New">
            <Children>
                <Child name="ChildA" />
            </Children>
        </Parent>
    </Parents>
    */
    
    XDocument xdoc = XDocument.Load(@"\path\to\xml\file");
    
    // get the first <child> element (the long way)
    XElement xeParents = xdoc.Descendants("Parents").FirstOrDefault();
    XElement xeParent = xeParents.Descendants("Parent").FirstOrDefault();
    XElement xeChildren = xeParent.Descendants("Children").FirstOrDefault();
    XElement xeChild = xeChildren.Descendants("Child").FirstOrDefault();
    
    //Change the value of an attribute
    xeParent.SetAttributeValue("id", "ParentB");
    
    //let's add another child for fun
    XElement xeChild2 = new XElement("Child");
    xeChild2.SetAttributeValue("name", "ChildB");
    xeChildren.Add(xeChild2);
    
    xdoc.Save(@"\path\to\save\file");
    
    
    
    //a shorter way to get the first <Child> would be:
    xeChild = xdoc.Descendants("Child").FirstOrDefault();
    
    //now the XML would look like this: 
    /*      
    <Parents>
        <Parent id="ParentB" description="New">
            <Children>
                <Child name="ChildA" />
                <Child name="ChildB" />
            </Children>
        </Parent>
    </Parents>
    */