为了账号安全,请及时绑定邮箱和手机立即绑定

使用 XDocument 从带有重复键的 XML 中读取

使用 XDocument 从带有重复键的 XML 中读取

C#
慕田峪4524236 2021-06-02 17:54:25
我需要一些有关 XDocument 的帮助。我有文件:<?xml version="1.0" encoding="utf-8" ?><SaleGroupsFiles>  <SaleGroup id="1" active="true" name="US">    <files      location="c:\mypath\"      fileType="pdf"      destination="outputpath">    </files>  </SaleGroup>  <SaleGroup id="2" active="true" name="Canada">    <files      location="c:\mypath\"      fileType="pdf"      destination="outputpath">    </files>  </SaleGroup></SaleGroups>我正在尝试使用 XDocument 读取文件static Dictionary<string, string> GetSettings(string path)    {        var document = XDocument.Load(path);        var root = document.Root;        var results =          root            .Elements()            .ToDictionary(element => element.Name.ToString(), element => element.Value);        return results;    }收到错误“字典中已存在具有该键的元素”。我猜这是因为“SaleGroup”重复了不止一次。读取数据的正确方法是什么?
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 122 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信