Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.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# 如何使用Linq从XML创建字典数组?_C#_Xml_Linq_Arrays_Dictionary - Fatal编程技术网

C# 如何使用Linq从XML创建字典数组?

C# 如何使用Linq从XML创建字典数组?,c#,xml,linq,arrays,dictionary,C#,Xml,Linq,Arrays,Dictionary,给定以下XML结构: <courses> <course> <title>foo</title> <description>bar</description> </course> ... </courses> 产生: [0] => {[course, foo]} [1] => {[description, bar]} 我想要的是: [0] => {[c

给定以下XML结构:

<courses>
  <course>
    <title>foo</title>
    <description>bar</description>
  </course>
  ...
</courses>
产生:

[0] => {[course, foo]}
[1] => {[description, bar]}
我想要的是:

[0] => {[course, foo], [description, bar]}
像这样:

x.Elements("course")
 .Select(c => c.Elements().ToDictionary(y => y.Name, y => y.Value))
 .ToArray();
像这样:

x.Elements("course")
 .Select(c => c.Elements().ToDictionary(y => y.Name, y => y.Value))
 .ToArray();

Visual Studio抱怨“System.Xml.Linq.XElement”不包含ToDictionary的定义。结果与我的问题中所示的不希望出现的结构相同-一个单条目字典数组。我尝试了,得到了一个双条目字典数组。哇,你说得对。我误解了VS调试器如何显示数据结构。非常感谢。Visual Studio抱怨“System.Xml.Linq.XElement”不包含ToDictionary的定义。结果与我的问题中所示的不希望出现的结构相同-一个单条目字典数组。我尝试了,得到了一个双条目字典数组。哇,你说得对。我误解了VS调试器如何显示数据结构。非常感谢。