将XmlDocument转换为Dictionary< string,string>

编程入门 行业动态 更新时间:2024-10-11 01:13:00
本文介绍了将XmlDocument转换为Dictionary< string,string>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在寻找一种使用Linq将XmlDocument转换为Dictionary<string, string>的好方法.

I'm looking for a good way to convert an XmlDocument to Dictionary<string, string> using Linq.

我的xml格式如下:

<config> <field>value</field> <field2>value2</field2> </config>

我想将其放入具有如下键值对的字典中:

I'd like to put this into a Dictionary with key value pairs looking like this:

字段,值 字段2,值2

field, value field2, value2

我想我应该使用Linq来做到这一点,但是我不确定语法.

I'm thinking I should use Linq to do this but I'm not sure about the syntax.

推荐答案

我不知道它是否是更好的方法,但它干净整洁.简单..

I dont know if its the better way, but its clean & simple..

XElement xdoc = XElement.Load("yourFilePath"); Dictionary<string, string> result = (from element in xdoc.Elements() select new KeyValuePair<string, string>(element.Name.ToString(), element.Value)).ToDictionary(x => x.Key, x => x.Value);

更多推荐

将XmlDocument转换为Dictionary&lt; string,string&gt;

本文发布于:2023-11-12 12:59:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581563.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   Dictionary   XmlDocument   amp   gt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!