Newbie to C# here….
I have the following code:
var xdoc = XDocument.Parse(xml);
var result = xdoc.Root.Elements("item")
.Select(itemElem => itemElem.Elements().ToDictionary(e => e.Name.LocalName, e => e.Value))
.ToList();
but when I try to use result as I would any List object, such as result.Item, it doesn’t work.
What am I doing wrong? Why is result not coming back as a normal List object that I can manipluate in my code? Do I need to make another List object from it?
I am just trying to get the first Dictionary item out of the List and use it.
It depends on what you expected. Your code currently produces a
List<Dictionary<string,string>>. So each entry in the list would be a dictionary.You can access each dictionary as you usually would access list elements, i.e.
The first part
[0]is the indexer of the list the second part["key"]is the indexer of the dictionary – this would return the value for the key"key"of the first dictionary in your list.This assumes the list has at least one entry though for which you would have to check.