Get all attributes in XML

c#, xml

Solution

var xDoc = XDocument.Load("path");

var attributes = xDoc.Descendants()
                 .SelectMany(x => x.Attributes())
                 .ToDictionary(x => x.Name.LocalName, x => (string)x);

Problem

I have an XML like this: ``` <msg action="getDetails" class="2"> <stamps msgtime="4/15/2014" ltq="2014-04-15"> <dat la="get" /> </stamps> </msg> ``` How can I retrieve Dictionary of all the attributes and their corresponding values? The expected output should look this: action - getDetails class - 2 msgtime - 4/15/2014 ltq - 2014-04-15 la - get I can get it work for a particular level, but not for all child elements.

Original source