如何扫描XElement并将所有元素(带有value)放入Dictionary中? [英] How to scan XElement and put all the element ( with value ) in Dictionary?

查看:89
本文介绍了如何扫描XElement并将所有元素(带有value)放入Dictionary中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的输入是XElement对象-我需要将此对象转换为Dictionary

My input is XElement object - and i need to convert this object to Dictionary

XElement看起来像这样

The XElement is look like this

 <Root>
    <child1>1</child1>
    <child2>2</child2>
    <child3>3</child3>
    <child4>4</child4>
 </Root>

我实际上需要返回的输出是

And the output that i actually need to return is

像这样的字典

[ Child1, 1 ]
[ Child2, 2 ]
[ Child3, 3 ]
[ Child4, 4 ]

我该怎么办?

感谢您的帮助.

推荐答案

您正在寻找ToDictionary()方法:

root.Elements().ToDictionary(x => x.Name.LocalName, x => x.Value)

这篇关于如何扫描XElement并将所有元素(带有value)放入Dictionary中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆