Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#中的字典数组可能吗?_C#_Arrays_Dictionary - Fatal编程技术网

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

C#中的字典数组可能吗?,c#,arrays,dictionary,C#,Arrays,Dictionary,我可以使用字典对象数组吗 我有一个XML,我想修改它。我将使用的数据结构如下所示: Dictionary<element, Dictionary<attr, value>> 字典 元素-是我将要修改的元素 attr-我要更新其值的属性 值-要更新的值 <Parents> <Parent id="ParentA" description="New"> <Children> <Chi

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

我有一个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
Dictionary dict_Attributes1=新字典();
Dictionary dict_Attributes2=新字典();
dict_attributes 1.添加(“id”、“ParentB”);
添加(“说明”、“旧”);
添加(“姓名”、“子女B”);
Dictionary dict_Elements=新字典();
添加(“.”,dict_属性1)//更新元素的步骤
添加(“儿童/儿童”,dict_属性2)//更新子对象的属性的步骤
假设我已经确定要更新id为ParentA的父项

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

或者,是否有更好的方法做到这一点: 1.是否修改选定元素及其子元素的属性

编辑


现在,当用户更改id和描述时,我想用新值更新这个XML文件。在更改id和描述(从用户处获得)时,我还想用相同的id值更新systemName

如果新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>

有比阵列更好的选项,它们将按照我认为您想要的方式运行:

var dictionaries = new List<Dictionary<string, string>>();
var字典=新列表();
这是动态调整大小的,无需将内容重新复制到新数组。与向词典中添加新条目的方式类似,您可以向列表中添加新条目:

var dict = new Dictionary<string, string> { { "Key1", "Value1" }, };
dictionaries.Add(dict);
var dict=newdictionary{{“Key1”,“Value1”},};
字典。添加(dict);
关于你提出的具体问题:

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

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

字典数组如下所示:

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

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

您可以尝试使用
数组列表

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


或者,查看
System.Xml.Linq

中可用的类,虽然您想做的是允许的,但我个人会使用


这是一个轻微的学习过程,但如果您至少熟悉LINQ和XML,那么在稍微使用它之后,这将是有意义的。它可能比您建议的结构更有效,当然也更容易适应和维护(由您或其他人),因为它们是一种标准化技术。

是的,这是可能的。我不确定您需要哪一个,但它们都使用
列表

var list=newlist();
或:

var list=newlist();

如果这样做的目的是编辑XML文件,那么您使用了错误的工具。我强烈建议使用Linq转换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>
*/
使用System.Xml.Linq;
//从您提供的XML开始:
/*
*/
XDocument xdoc=XDocument.Load(@“\path\to\xml\file”);
//获取第一个元素(漫长的道路)
XElement xeParents=xdoc.subjects(“Parents”).FirstOrDefault();
XElement xeParent=xeParents.subjects(“Parent”).FirstOrDefault();
XElement xeChildren=xeParent.subjections(“Children”).FirstOrDefault();
XElement xeChild=xeChildren.subjections(“Child”).FirstOrDefault();
//更改属性的值
SetAttributeValue(“id”、“ParentB”);
//让我们添加另一个孩子来娱乐
XElement xeChild2=新XElement(“子”);
xeChild2.SetAttributeValue(“名称”、“ChildB”);
xeChildren.Add(xeChild2);
xdoc.Save(@“\path\to\Save\file”);
//获得第一个的较短方法是:
xeChild=xdoc.substands(“Child”).FirstOrDefault();
//现在,XML将如下所示:
/*      
*/

Merlyn:谢谢。但是怎么做呢?我在谷歌上搜索了一下(接着是Bing),但没有结果。一个代码示例,一个指向代码示例的链接…任何事情都可以。只需使用
列表
@Kanini:一些事情-您可以使用XSLT来完成这项工作,而不是尝试实现您自己的通用方法来指定文件应如何更新。除此之外,我建议专门编写代码来处理这种情况,而不是期望代码的用户构造一个带有选项路径的字典。更好的方法是,使用XML序列化,让他们编辑类的属性,而不是关心数据的存储方式。@Kanini:别误会,新值的XPath字典是一个有趣的选项,但是有更简单(编码和使用)和更不容易出错的编程方法。@Kanini:您最好使用XSLT(正如梅林所说)或者林克托-XML@Kanini:,.BTW,w3schools非常适合HTML/XML
var list = new List<Dictionary<string, string>>();
var list = new List<Dictionary<string, Dictionary<string, string>>>();
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>
*/