C# 在字典中读取XML时出错

C# 在字典中读取XML时出错,c#,xml,C#,Xml,我有这样一个XML文件: <APICollection><API name="myapiName" message="myAPIMessage" /></APICollection> Dictionary<string,string> apiMap = new Dictionary<string,string>(); XDocument xdoc = XDocument.Load(path); apiMap = xdo

我有这样一个XML文件:

<APICollection><API name="myapiName" message="myAPIMessage" /></APICollection>
Dictionary<string,string> apiMap = new Dictionary<string,string>();    
XDocument xdoc = XDocument.Load(path);    
apiMap = xdoc.Root.Elements()
              .ToDictionary(a => (string)a.Attribute("apiName"), a => (string)a.Attribute("apiMessage"));
An item with the same key has already been added.
var values = xmlDocument
             .Descendants("API")
             .GroupBy(x => (string)x.Attribute("name"))
             .ToDictionary(x => x.Key, 
                           x => (string)x.First().Attribute("message"));
XDocument xdoc = XDocument.Load(path);    
var apiMap = xdoc.Root.Elements()
                 .Select(a => new
                     {
                         ApiName = (string)a.Attribute("apiName"),
                         ApiMessage = (string)a.Attribute("apiMessage")
                     });
var duplicateKeys = (from x in apiMap
                     group x by x.ApiName into g
                     select new { ApiName = g.Key, Cnt = g.Count() })
                     .Where(x => x.Cnt > 1)
                     .Select(x => x.ApiName);
if (duplicateKeys.Count() > 0)
    throw new InvalidOperationException("The following keys are present more than once: " 
            + string.Join(", ", duplicateKeys));

Dictionary<string, string> apiMapDict = 
    apiMap.ToDictionary(a => a.ApiName, a => a.ApiMessage);
我想知道我能做些什么来修改错误消息,以至少提供多次出现的密钥。有人能帮忙吗

谢谢


Harit

属性方法采用属性名称,而不是值。用属性名称替换这些值。还可以使用
Value
属性获取属性的值

即使这样做,作为键使用的属性值也必须是唯一的,因为您使用的是
字典

Dictionary apiMap=newdictionary();
XDocument xdoc=XDocument.Load(路径);
apiMap=xdoc.Root.Elements()
.ToDictionary(a=>a.Attribute(“name”).Value
,a=>a.Attribute(“message”).Value);
试试这个:

var xmlDocument = XDocument.Load(path); 
var values = xmlDocument
             .Descendants("API")
             .GroupBy(x => (string)x.Attribute("name"))
             .ToDictionary(x => x.Key, 
                           x => x.Select(p => (string)p.Attribute("message").ToList());
这将为您提供一个
字典
,键是名称,值是属于Api名称的消息。如果您只需要字典,请按如下方式更改代码:

<APICollection><API name="myapiName" message="myAPIMessage" /></APICollection>
Dictionary<string,string> apiMap = new Dictionary<string,string>();    
XDocument xdoc = XDocument.Load(path);    
apiMap = xdoc.Root.Elements()
              .ToDictionary(a => (string)a.Attribute("apiName"), a => (string)a.Attribute("apiMessage"));
An item with the same key has already been added.
var values = xmlDocument
             .Descendants("API")
             .GroupBy(x => (string)x.Attribute("name"))
             .ToDictionary(x => x.Key, 
                           x => (string)x.First().Attribute("message"));
XDocument xdoc = XDocument.Load(path);    
var apiMap = xdoc.Root.Elements()
                 .Select(a => new
                     {
                         ApiName = (string)a.Attribute("apiName"),
                         ApiMessage = (string)a.Attribute("apiMessage")
                     });
var duplicateKeys = (from x in apiMap
                     group x by x.ApiName into g
                     select new { ApiName = g.Key, Cnt = g.Count() })
                     .Where(x => x.Cnt > 1)
                     .Select(x => x.ApiName);
if (duplicateKeys.Count() > 0)
    throw new InvalidOperationException("The following keys are present more than once: " 
            + string.Join(", ", duplicateKeys));

Dictionary<string, string> apiMapDict = 
    apiMap.ToDictionary(a => a.ApiName, a => a.ApiMessage);

我建议不要直接创建字典,而是首先检查重复键,如下所示:

<APICollection><API name="myapiName" message="myAPIMessage" /></APICollection>
Dictionary<string,string> apiMap = new Dictionary<string,string>();    
XDocument xdoc = XDocument.Load(path);    
apiMap = xdoc.Root.Elements()
              .ToDictionary(a => (string)a.Attribute("apiName"), a => (string)a.Attribute("apiMessage"));
An item with the same key has already been added.
var values = xmlDocument
             .Descendants("API")
             .GroupBy(x => (string)x.Attribute("name"))
             .ToDictionary(x => x.Key, 
                           x => (string)x.First().Attribute("message"));
XDocument xdoc = XDocument.Load(path);    
var apiMap = xdoc.Root.Elements()
                 .Select(a => new
                     {
                         ApiName = (string)a.Attribute("apiName"),
                         ApiMessage = (string)a.Attribute("apiMessage")
                     });
var duplicateKeys = (from x in apiMap
                     group x by x.ApiName into g
                     select new { ApiName = g.Key, Cnt = g.Count() })
                     .Where(x => x.Cnt > 1)
                     .Select(x => x.ApiName);
if (duplicateKeys.Count() > 0)
    throw new InvalidOperationException("The following keys are present more than once: " 
            + string.Join(", ", duplicateKeys));

Dictionary<string, string> apiMapDict = 
    apiMap.ToDictionary(a => a.ApiName, a => a.ApiMessage);
XDocument xdoc=XDocument.Load(路径);
var apiMap=xdoc.Root.Elements()
.选择(a=>新建
{
ApiName=(字符串)a.Attribute(“ApiName”),
ApiMessage=(字符串)a.Attribute(“ApiMessage”)
});
var duplicateKeys=(来自apiMap中的x)
按x.ApiName将x分组为g
选择新{ApiName=g.Key,Cnt=g.Count()})
.其中(x=>x.Cnt>1)
.选择(x=>x.ApiName);
如果(duplicateKeys.Count()>0)
抛出新的InvalidOperationException(“以下键多次出现:”
+Join(“,”,duplicateKeys));
字典apiMapDict=
ToDictionary(a=>a.ApiName,a=>a.ApiMessage);
请注意,我已经更改了代码,您的示例(“名称”、“消息”)中的属性名称与代码示例(“apiName”、“apiMessage”)中的属性名称存在差异