繁体   English   中英

C#中的字典数组-可能吗?

[英]An Array of Dictionary in C# - Possible?

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

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

Dictionary<element, Dictionary<attr, value>>

element-是我要修改的Element attr-我要更新其值的属性value-我要更新的值

<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的Parent。

现在,在这里我要创建dict_Attributes1,dict_Attributes2等,是否可以将它们存储在(动态的,编译时未知的大小)Dictionary对象数组中?

另外,还有更好的方法吗?1.修改Selected 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值更新systemName。

如果新ID为“ Development”,说明为“ DevelopmentSystem”,

输出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>

对的,这是可能的。 我不确定您需要哪个,但是它们都使用List<T>

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

要么:

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

尽管您想做的是允许的,但我个人将使用LINQ to XML

虽然学习曲线有些许变化,但是如果您至少对LINQ和XML有所了解,那么在稍微使用一下之后就可以了。 它可能比您建议的结构更有效,并且由于它们是一种标准化技术,因此肯定会更容易(由您,尤其是由其他人)进行适应和维护。

有比数组更好的选择,它们会按照我认为的方式运行:

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

它是动态可调整大小的,而不必将内容重新复制到新数组中。 与可以将新条目添加到字典的方式类似,可以将新项目添加到列表中:

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

至于你问的具体问题:

现在,在这里我要创建dict_Attributes1,dict_Attributes2等,是否可以将它们存储在(动态的,编译时未知的大小)Dictionary对象数组中?

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

字典的数组如下所示:

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

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

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

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

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

如果这样做的目的是编辑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>
*/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM