C# 如何使用Linq从XML创建字典数组?
给定以下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
<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调试器如何显示数据结构。非常感谢。