Ok, I\'ve got the following XML tree
1000
You can find the leaves by looking for elements that have no descendants:
var doc = XDocument.Load(fileName);
var leaves =
from e in doc.Descendants()
where !e.Elements().Any()
select e;
I don't know if there is a built-in way to get the path of an element, but you can easily create an extension method to build it:
static class Extensions
{
public static string Path(this XElement element)
{
XElement tmp = element;
string path = string.Empty;
while (tmp != null)
{
path = "/" + tmp.Name + path;
tmp = tmp.Parent;
}
return path;
}
}
You can then build the dictionary like this:
var dict = leaves.ToDictionary(e => e.Path(), e => e.Value);