title: IXmlSerializable list item
body: <p>For fun, I'm trying to develop a simple RPG. I would like my game to use XML files in order to make the game easily customizable by players.</p>
  
  <p>I got a race class and a raceManager class with a static list of all races.
  I started by using XmlAttributs (XmlRoot, XmlElement...)  and the default serializer to write the playable races to Xml. </p>
  
  <pre><code>XmlSerializer xs = new XmlSerializer(managedList.GetType());
  using (FileStream fs = new FileStream(filePath, FileMode.Create))
  {
      xs.Serialize(fs, managedList);
  }
  </code></pre>
  
  <p>It works but in my race class, I have lists for skills, weapons usable...
  The default serializer generate arrays containing all the properties of these classes.</p>
  
  <p>As I only want a single instance of each object, I'm starting to implement the IXmlSerializable interface to write only id and retrieve object when i read.</p>
  
  <p>Now the Xml file is almost like I wanted :</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;ArrayOfRace xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;Race&gt;
      &lt;NameMale&gt;Human&lt;/NameMale&gt;
      ...
      &lt;ArmorsTypesAllowed&gt;
        &lt;ArmorType&gt;Cloth&lt;/ArmorType&gt;
        &lt;ArmorType&gt;Leather&lt;/ArmorType&gt;
        &lt;ArmorType&gt;Metal&lt;/ArmorType&gt;
      &lt;/ArmorsTypesAllowed&gt;
    &lt;/Race&gt;
    &lt;Race&gt;
      &lt;NameMale&gt;Dwarf&lt;/NameMale&gt;
      ...
    &lt;/Race&gt;
  </code></pre>
  
  <p>...
      </p>
  
  <p>But I can no longer read it :'(</p>
  
  <p>I use my manager method (which worked fine before): </p>
  
  <pre><code>protected static List&lt;T&gt; ReadFile(string filePath)
  {
      List&lt;T&gt; ManagedList = new List&lt;T&gt;();
      XmlSerializer xs = new XmlSerializer(ManagedList.GetType());
      using (FileStream fs = new FileStream(filePath, FileMode.Open))
      {
          ManagedList = (List&lt;T&gt;)xs.Deserialize(fs);
      }
      return ManagedList;
  }
  </code></pre>
  
  <p>The ReadXml method is used but return a list of 1 element (the last one).
  Looks like the whole file (contaning the list) is send to my ReadXml which is suppose to read only one element of that list.</p>
  
  <pre><code>public void ReadXml(XmlReader reader)
  {
      while(!reader.EOF)
      {
          if(reader.NodeType == XmlNodeType.Element)
          {
              switch(reader.Name)
              {
                  case "NameMale":
                      NameMale = reader.ReadElementContentAsString();
                      break;
                  case "NameFemale":
                      NameFemale = reader.ReadElementContentAsString();
                      break;
                      ...
                  default:
                      reader.Read();
                      break;
              }
          }
          else
          {
              reader.Read();
          }       
      }
  }
  </code></pre>
  
  <p>I thought I should perhaps stop the reader. So I added the following code but it didn't solve my problem (only the first race is retrieve with this)</p>
  
  <pre><code>if(reader.NodeType == XmlNodeType.EndElement &amp;&amp; reader.Name == "Race")
  {
      return;
  }
  </code></pre>
  
  <p>Thank you in advance and sorry for my poor English.</p>
  
  <h2>Edit : Here is a minimal but complete example with a console app :</h2>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Linq;
  using System.Xml;
  using System.Xml.Schema;
  using System.Xml.Serialization;
  
  namespace StackOverFlowQuestion
  {
      class Program
      {
          static void Main(string[] args)
          {
              if (!File.Exists(Manager.FilePath))
              {
                  Manager.WriteFile(Manager.FilePath, Manager.GenerateMyClass());
              }
  
              Manager.Initialize();
              foreach (MyClass mc in Manager.ListOfMyClass)
              {
                  Console.WriteLine("A:" + mc.A);
                  Console.WriteLine("B:" + mc.B);
                  foreach(AnotherClass ac in mc.MyList)
                  {
                      Console.WriteLine("Item ID: " + ac.Id);
                  }
              }
              Console.ReadLine();
          }
      }
  
  
      public class MyClass : IXmlSerializable
      {
          public string A { get; set; }
          public string B { get; set; }
          public List&lt;AnotherClass&gt; MyList { get; set; }
  
          public MyClass() { }
  
          public MyClass(string a, string b, List&lt;AnotherClass&gt; list)
          {
              this.A = a;
              this.B = b;
              this.MyList = list;
          }
  
          public XmlSchema GetSchema()
          {
              return null;
          }
  
          public void ReadXml(XmlReader reader)
          {
              while (!reader.EOF)
              {
                  if (reader.NodeType == XmlNodeType.Element)
                  {
                      switch (reader.Name)
                      {
                          case "A":
                              A = reader.ReadElementContentAsString();
                              break;
                          case "B":
                              B = reader.ReadElementContentAsString();
                              break;
                          case "MyList":
                              MyList = new List&lt;AnotherClass&gt;();
                              reader.Read();
                              break;
                          case "ListItem":
                              string s = reader.ReadElementContentAsString();
                              MyList.Add(Manager.ListOfAnotherClass.Single(x =&gt; x.Id == s));
                              break;
                          default:
                              reader.Read();
                              break;
                      }
                  }
                  else
                  {
                      reader.Read();
                  }
  
              }
          }
  
          public void WriteXml(XmlWriter writer)
          {
              writer.WriteElementString("A", A);
              writer.WriteElementString("B", B);
  
              writer.WriteComment("Only Id must be serialize !");
              writer.WriteStartElement("MyList");
              if (MyList != null)
              {
                  foreach (AnotherClass ac in MyList)
                  {
                      writer.WriteElementString("ListItem", ac.Id);
                  }
              }
              writer.WriteEndElement();
          }
      }
  
      public class AnotherClass
      {
          public string Id { get; set; }
          public string someProperty { get; set; }
  
          public AnotherClass(string id, string prop)
          {
              this.Id = id;
              this.someProperty = prop;
          }
      }
  
  
      class Manager
      {
          public static List&lt;MyClass&gt; ListOfMyClass { get; set; }
          public static string FilePath = "MyXmlFile.xml";
  
          //This list should be from another XML file.
          private static List&lt;AnotherClass&gt; _listofAnotherClass;
          public static List&lt;AnotherClass&gt; ListOfAnotherClass
          {
              get
              {
                  if (_listofAnotherClass == null)
                  {
                      _listofAnotherClass = GenerateAnotherClass();
                  }
                  return _listofAnotherClass;
              }
          }
  
          public static void Initialize()
          {
              ListOfMyClass = ReadFile(Manager.FilePath);
          }
  
          public static List&lt;MyClass&gt; ReadFile(string filePath)
          {
              List&lt;MyClass&gt; ManagedList = new List&lt;MyClass&gt;();
              XmlSerializer xs = new XmlSerializer(ManagedList.GetType());
              using (FileStream fs = new FileStream(filePath, FileMode.Open))
              {
                  ManagedList = (List&lt;MyClass&gt;)xs.Deserialize(fs);
              }
              return ManagedList;
          }
  
  
          public static void WriteFile(string filePath, List&lt;MyClass&gt; managedList)
          {
              XmlSerializer xs = new XmlSerializer(managedList.GetType());
              using (FileStream fs = new FileStream(filePath, FileMode.Create))
              {
                  xs.Serialize(fs, managedList);
              }
          }
  
          public static List&lt;MyClass&gt; GenerateMyClass()
          {
              List&lt;MyClass&gt; list = new List&lt;MyClass&gt;();
              MyClass m;
  
              m = new MyClass("ValueA", "ValueB", new List&lt;AnotherClass&gt;(Manager.ListOfAnotherClass.Where(x =&gt; x.Id == "Id1" || x.Id == "Id3")));
              list.Add(m);
  
              m = new MyClass("ValA", "ValB", new List&lt;AnotherClass&gt;(Manager.ListOfAnotherClass.Where(x =&gt; x.Id == "Id2" || x.Id == "Id3")));
              list.Add(m);
  
              return list;
          }
  
          public static List&lt;AnotherClass&gt; GenerateAnotherClass()
          {
              List&lt;AnotherClass&gt; anotherList = new List&lt;AnotherClass&gt;();
              anotherList.Add(new AnotherClass("Id1", "Prop1"));
              anotherList.Add(new AnotherClass("Id2", "Prop2"));
              anotherList.Add(new AnotherClass("Id3", "Prop3"));
              return anotherList;
          }
      }
  }
  </code></pre>