• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

添加XML文件并读取键值对到字典

用户头像
it1352
帮助1

问题说明

我是相当新的节目。我想补充一个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?

正确答案

#1

您可以使用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"));

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhchaaki
系列文章
更多 icon
同类精品
更多 icon
继续加载