score:2

This is untested, but should do the trick:

panel
    .Elements("HeaderColumns")
    .Elements("column")
    .ToDictionary(
        column=>(int)column.Attribute("performanceId"),
        column=>(string)column.Attribute("text")
    )

In this case I am using the following overload of ToDictionary:

public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement>(
    this IEnumerable<TSource> source,
    Func<TSource, TKey> keySelector,
    Func<TSource, TElement> elementSelector
)

So, here source would be:

panel
    .Elements("HeaderColumns")
    .Elements("column")

And keySelector and elementSelector are functions (delegates) that take an element of type TSource (from source), and return a value derived from the element:

keySelector:

        column=>(int)column.Attribute("performanceId")

elementSelector:

        column=>(string)column.Attribute("text")

These are declared in lambda format. The type of the dictionary is then inferred from the return types of these delegates. This would be Dictionary<int,string> in this case.

The generic delegate Func<TSource, TKey> would define the type of a method that takes a parameter of type TSource and returns a value of type TKey. It's very important to get to grips with exactly what this means to have a productive time using the Linq extension methods.

score:1

It is hard to give a complete answer as your xml and C# don't match up; but from each "HeaderColumns" you can use:

var columns = from column in headerColumns.Elements("column")
              select new {
                  PerformanceId = (int)column.Attribute("performanceId"),
                  Text = (string)column.Attribute("text")
              };
Dictionary<int, string> myHeaders = columns.ToDictionary(
    column => column.PerformanceId, column => column.Text);

If you have multiple <HeaderColumns>, then SelectMany maybe...

var columns = from panel in doc.Elements("PerformancePanel")
              from headers in panel.Elements("HeaderColumns")
              from column in headers.Elements("column")
              select new {
                  PerformanceId = (int)column.Attribute("performanceId"),
                  Text = (string)column.Attribute("text")
              };
Dictionary<int, string> myHeaders = columns.ToDictionary(
    column => column.PerformanceId, column => column.Text);

Related Articles