如何添加一个XML文件并读取键值对到字典? [英] How to add an XML file and read key value pair into a dictionary?

查看:521
本文介绍了如何添加一个XML文件并读取键值对到字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的节目。我想补充一个XML文件,以存储一些映射。我想在字典中准备这些键值对。以下是我想到的是XML格式:

I am fairly new to programming. I am trying to add an XML file in order to store some mappings. I want to ready these key value pairs in a dictionary. Following is the format of the XML I am thinking:

<?xml version="1.0" encoding="utf-8" ?>
<Map>
  <add keyword="keyword1" replaceWith="replaceMe1"/>
  <add keyword="keyword2" replaceWith="replaceMe2"/>  
</Map>



能否请你告诉我,如果格式是正确的? ?如果是,我将如何读入到我的C#字典

Can you please tell me if the format is correct? If it is, how would I read it into my C# dictionary?

推荐答案

您可以使用LINQ到XML:

You can use LINQ to XML:

var xdoc = XDocument.Load(path_to_xml);
var map = xdoc.Root.Elements()
                   .ToDictionary(a => (string)a.Attribute("keyword"),
                                 a => (string)a.Attribute("replaceWith"));

这篇关于如何添加一个XML文件并读取键值对到字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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