2009-11-25 40 views
0

我的XML看起来像:试图XML转换为词典

<root> 
    <blah1>some text</blah1> 
    <someother>blah aasdf</someother> 
</root> 

我想将其转换为一个字典

所以我可以做:

myDict["blah1"] 

它返回文字'一些文字'

到目前为止我有:

Dictionary<string,string> myDict = (from elem in myXmlDoc.Element("Root").Elements() 
           select elem.Value).ToDictionary<string,string>(); 

是正确的还是必须将选择更改为2个结果?

回答

2

指定要用于密钥什么,什么价值。

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