LINQ has a ToDictionary extension method on IEnumerables. Consider using it on doc.Root.Descendants and get it rid of the for-loop below.