title: Using LINQ to turn XML into Dictionary when XML file entries vary in format
body: <p>I have the following XML (excerpt):</p>
  
  <pre><code>&lt;Offsets&gt;
    &lt;PlayerStructBase&gt;0xF24C10&lt;/PlayerStructBase&gt;
    &lt;HP&gt;
      &lt;offset&gt;0x17e8&lt;/offset&gt;
    &lt;/HP&gt;
    &lt;MaxHP&gt;
      &lt;offset&gt;0x17ec&lt;/offset&gt;
    &lt;/MaxHP&gt;
    &lt;MP&gt;
      &lt;offset&gt;0x17f0&lt;/offset&gt;
    &lt;/MP&gt;
    &lt;DistanceToTarget&gt;
      &lt;offset&gt;0xEB9A98&lt;/offset&gt;
      &lt;offset&gt;0x17f0&lt;/offset&gt;
    &lt;/DistanceToTarget&gt;
  &lt;/Offsets&gt;
  </code></pre>
  
  <p>I would like to use LINQ to read it, then turn it into a Dictionary. What I could not figure using other posts / Google was how to do this when each entry (HP, MP, etc.) might have anything from one to multiple offsets, and they all have the same element name: "offset").</p>
  
  <p>Any bright ideas?</p>
  
  <p><strong>EDIT:</strong>
  I forgot to mention that the output Dictionary should be: <code>Dictionary&lt;string, IntPtr&gt;</code>. I have a method that converts a int[] to IntPtr in a loop: pointers[i] = Utils.getPointerFromBaseOffsets(ProcessHandle, _baseAddress, offsets[i]);. Perhaps there is some way to set these IntPtr's directly while creating the dictionary? Or do I first have to fill it with int[] and then in a loop overwrite each value? If the latter, how would i do that?</p>
  
  <hr>
  
  <p><strong>UPDATE:</strong> I ended up modifying the XML so the PlayerStructBase hex was wrapped in an <em>offset</em> element, like the other entries in the XML. Then I just did this:</p>
  
  <pre><code>//Load Offset list
  var offsetXElement = XDocument.Load(path + @"\Offsets.xml").Root;
  if (offsetXElement != null)
      OffsetDictionary =
          offsetXElement.Elements()
              .ToDictionary(o =&gt; o.Name.LocalName,
                  o =&gt; MemoryHandler.Instance.GetPointerFromBaseOffsets(o.Elements().Select(x =&gt; int.Parse(x.Value.Substring(2), NumberStyles.AllowHexSpecifier)).ToArray()));
  </code></pre>