C# 正在尝试将xml转换为字典

C# 正在尝试将xml转换为字典,c#,xml,linq,dictionary,C#,Xml,Linq,Dictionary,我的xml看起来像: <root> <blah1>some text</blah1> <someother>blah aasdf</someother> </root> 然后返回文本“some text” 到目前为止,我已经: Dictionary<string,string> myDict = (from elem in myXmlDoc.Element("Root").Elements()

我的xml看起来像:

<root>
  <blah1>some text</blah1>
  <someother>blah aasdf</someother>
</root>
然后返回文本“some text”

到目前为止,我已经:

Dictionary<string,string> myDict = (from elem in myXmlDoc.Element("Root").Elements()
                                select elem.Value).ToDictionary<string,string>();
Dictionary myDict=(来自myXmlDoc.Element(“Root”).Elements()中的elem)
选择elem.Value).ToDictionary();

这是正确的,还是我必须将select更改为具有2个结果的内容?

您需要在ToDictionary调用中使用lambda,以便它知道对键使用什么以及对值使用什么


在ToDictionary调用中需要一个lambda,以便它知道该键使用什么,值使用什么


指定键和值的内容

var myDict = myXmlDoc.Elements()
                     .ToDictionary( key => key.Name, val => val.Value);

指定键和值所需的内容

var myDict = myXmlDoc.Elements()
                     .ToDictionary( key => key.Name, val => val.Value);
myXmlDoc.Root
    .Elements()
    .ToDictionary(xe => xe.Name, xe => xe.Value);