title: c# parse xml array with attribute
body: <p>I'm trying to parse an xml array, that has an attribute in a weird place.</p>
  
  <p>Here is my(found somewhere on stackowerflow) parser code:</p>
  
  <pre><code>public static string Serialize (object objectToSerialize)
  {
      MemoryStream mem = new MemoryStream ();          
      XmlSerializer ser = new XmlSerializer (objectToSerialize.GetType ());         
      ser.Serialize (mem, objectToSerialize);     
      UTF8Encoding utf = new UTF8Encoding ();
      return utf.GetString (mem.ToArray ());
  }
  
  public static T Deserialize&lt;T&gt; (string xmlString)
  {
      byte[] bytes = Encoding.UTF8.GetBytes (xmlString);
      MemoryStream mem = new MemoryStream (bytes);         
      XmlSerializer ser = new XmlSerializer (typeof(T));
      return (T)ser.Deserialize (mem);
  }
  </code></pre>
  
  <p>This works well in all cases(that I need), except here:</p>
  
  <pre><code>&lt;hr&gt;
      &lt;type n="easy"&gt;
          &lt;scores&gt;
              &lt;country name="USA"&gt;
                  &lt;score nickname="steve" rank="1"&gt;1982&lt;/score&gt;
                  &lt;score nickname="bill" rank="2"&gt;1978&lt;/score&gt;
  ...
  </code></pre>
  
  <p>This is my template class:</p>
  
  <pre><code>public class CountryList
  {
      public CountryList ()
      {
      }
  
      [XmlArray("country")]
      [XmlArrayItem("score")]
      public ScoreAndRank[]
          country;
  
      [XmlAttribute("name")]
      public string
          name;
      }
  }
  </code></pre>
  
  <p>The score array is parsed correctly, but "name" is always null. 
  I can even get  the value of n out, (as [XmlAttribute("n")]).
  Any tips how to solve this?</p>
  
  <p>edit:solution based on Svein Fidjestøl's link</p>
  
  <pre><code>[XmlType("country")]
  public class ScoreContainer
  {
      public ScoreContainer ()
      {
      }
      [XmlElement("score")]
      public ScoreAndRank[] country;
  
      [XmlAttribute("name")]
      public string name;
  
  }
  </code></pre>
  
  <p>Works like a charm.</p>