title: How to parse XML data into the properties of a custom C# class?
body: <h2>Setup</h2>
  
  <p>I have this in my <code>Main()</code> function.</p>
  
  <pre><code>List&lt;Token&gt; tokens = new List&lt;Token&gt;();
  string path = @"\(some directories)\tokens.xml";
  XDocument doc = XDocument.Load(path);
  </code></pre>
  
  <p>I have this class with a few properties.</p>
  
  <pre><code>public partial class Token
  {
      public Token()
      {
          SetURLs = new List&lt;string&gt;();
          SetNames = new List&lt;string&gt;();
      }
  
      public string Name { get; set; }
      public List&lt;string&gt; SetURLs { get; set; }
      public List&lt;string&gt; SetNames { get; set; }
      public string Color { get; set; }
      public string PT { get; set; }
      public string Text { get; set; }
  }
  </code></pre>
  
  <p>I have this XML file. Here is a snippet.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; //EDIT3
  &lt;card_database version="2"&gt;            //EDIT3
      &lt;cards&gt;
          .
          .
          .
          &lt;card&gt;
              &lt;name&gt;Griffin&lt;/name&gt;
              &lt;set picURL="http://magiccards.info/extras/token/duel-decks-ajani-vs-nicol-bolas/griffin.jpg" picURLHq="" picURLSt=""&gt;DDH&lt;/set&gt;
              &lt;color&gt;w&lt;/color&gt;
              &lt;manacost&gt;&lt;/manacost&gt;
              &lt;type&gt;Token&lt;/type&gt;
              &lt;pt&gt;2/2&lt;/pt&gt;
              &lt;tablerow&gt;0&lt;/tablerow&gt;
              &lt;text&gt;Flying&lt;/text&gt;
              &lt;token&gt;1&lt;/token&gt;
          &lt;/card&gt;
          &lt;card&gt;
              &lt;name&gt;Rat&lt;/name&gt;
              &lt;set picURL="http://magiccards.info/extras/token/shadowmoor/rat.jpg" picURLHq="" picURLSt=""&gt;SHM&lt;/set&gt;
              &lt;set picURL="http://magiccards.info/extras/token/gatecrash/rat.jpg" picURLHq="" picURLSt=""&gt;GTC&lt;/set&gt;
              &lt;color&gt;b&lt;/color&gt;
              &lt;manacost&gt;&lt;/manacost&gt;
              &lt;type&gt;Token&lt;/type&gt;
              &lt;pt&gt;1/1&lt;/pt&gt;
              &lt;tablerow&gt;0&lt;/tablerow&gt;
              &lt;text&gt;&lt;/text&gt;
              &lt;token&gt;1&lt;/token&gt;
          &lt;/card&gt;
          .
          .
          .
      &lt;/cards&gt;
  &lt;/card_database&gt; //EDIT3
  </code></pre>
  
  <p>As you can see, there are many <code>&lt;card&gt;</code> elements in the <code>&lt;cards&gt;</code> root element. Further, each <code>&lt;card&gt;</code> can have many <code>&lt;set&gt;</code> elements. I made the class accordingly.</p>
  
  <h2>Problem</h2>
  
  <p>How do I go through one <code>&lt;card&gt;</code> at a time and assign the appropriate values to each property?</p>
  
  <h2>What I Have Tried</h2>
  
  <p>I made a list that contains all of the <code>&lt;name&gt;</code> elements of each <code>&lt;card&gt;</code>. I would then go through this list and assign a name to a new instance of the <code>Token</code> class's <code>Name</code> property. Then populate my <code>tokens</code> list with each new instance.</p>
  
  <pre><code>List&lt;string&gt; names = new List&lt;string&gt;();
  names = doc.Descendants("card").Elements("name").Select(r =&gt; r.Value).ToList();
  int amount = doc.Descendants("card").Count();
  
  for(int i = 0; i &lt; amount; i++)
  {
      Token token = new Token();
  
      token.Name = name[i];
  
      .
      .
      .
  
      tokens.Add(token);
  }
  </code></pre>
  
  <p>I guess I could then make more lists that contain every other desired element and do the same process, but there has to be a more elegant way, right?</p>
  
  <p>EDIT</p>
  
  <p>I also tried serialization from another question. But for some reason, when I tried to write something from <code>token</code> to the console (say token.Name), it didn't write anything.</p>
  
  <pre><code>XmlSerializer serializer = new XmlSerializer(typeof(Token));
  using (StringReader reader = new StringReader(path))
  {
      Token token = (Token)(serializer.Deserialize(reader));
  }
  </code></pre>
  
  <p>Probably just an incorrect implementation. If that is the case, could someone use what I posted and show me the correct implementation? Also, I assume this will give 1 or many values to my two <code>List</code> properties, right?</p>
  
  <p>EDIT</p>
  
  <p>Thanks for the help.</p>
  
  <p>EDIT2</p>
  
  <h2>An Answer</h2>
  
  <p>After some unsuccessful fiddling with serialization and some more searching, I made an implementation that works.</p>
  
  <pre><code>foreach (var card in doc.Descendants("card"))
  {
      Token token = new Token();
  
      token.Name = card.Element("name").Value.ToString();
      foreach (var set in card.Elements("set"))
      {
          token.SetURLs.Add(set.Attribute("picURL").Value.ToString());
          token.SetNames.Add(set.Value.ToString());
      }
      token.Color = card.Element("color").Value.ToString();
      token.PT = card.Element("pt").Value.ToString();
      token.Text = card.Element("text").Value.ToString();
  
      tokens.Add(token);
  }
  </code></pre>
  
  <p>Much better than the number of <code>Lists</code> I first had in mind. Not as succinct as the serialization might have been. However, it does what I need.</p>
  
  <p>Thanks for the help.</p>
  
  <p>EDIT4</p>
  
  <p>Not sure if this many edits are allowed or against etiquette. Just wanted to make this edit for future readers.</p>
  
  <p>The stuff under the "An Answer" section does solve my problem but the XML Serialization posted below by Dave is much better; it is more flexible and easier to reuse/modify. So, pick the solution that has more benefits for your situation.</p>