title: Generic serialization of lists in C#
body: <p>I am trying to serialize several lists of objects to xml.  The lists are of different types but they all need to have some the same attributes on the top list object.</p>
  
  <p>What I am trying to get is a 'count' on the top level and the name of the object for all the items in the list:</p>
  
  <pre><code> &lt;JobResult Count="2"&gt;
         &lt;Job&gt;
             &lt;Id&gt;1&lt;/Id&gt;
         &lt;/Job&gt;
         &lt;Job&gt;
             &lt;Id&gt;2&lt;/Id&gt;
         &lt;/Job&gt;
      &lt;/JobResult&gt;
  </code></pre>
  
  <p>Then for another list:</p>
  
  <pre><code> &lt;PersonResult Count="1"&gt;
         &lt;Person&gt;
             &lt;Id&gt;1&lt;/Id&gt;
         &lt;/Person&gt;
   &lt;/PersonResult&gt;
  </code></pre>
  
  <p>The code I am using is:</p>
  
  <pre><code>[XmlRoot()]
      public class Result&lt;T&gt;
      {
          [XmlElement()]
          public List&lt;T&gt; Items { get; set; }
  
          public Result()
          {
              this.Items = new List&lt;T&gt;();
          }
  
          [XmlAttribute("Count")]
          public int ItemCount
          {
              get
              {
                  return this.Items.Count;
              }
              set
              {
  
              }
          }
      }
  var jobs= new Result&lt;Job&gt;();
  var persons= new Result&lt;Person&gt;();
  </code></pre>
  
  <p>What I am getting is:</p>
  
  <pre><code>&lt;ResultOfJob Count="2"&gt;
         &lt;Item&gt;
             &lt;Id&gt;1&lt;/Id&gt;
         &lt;/Item&gt;
         &lt;Item&gt;
             &lt;Id&gt;2&lt;/Id&gt;
         &lt;/Item&gt;
      &lt;/ResultOfJob &gt;
  </code></pre>
  
  <p>I have tried the attribute naming like this but get &lt;_x007B_0_x007D_> instead of item.</p>
  
  <pre><code>[XmlElement({0})]
   public List&lt;T&gt; Items { get; set; }
  </code></pre>
  
  <p>What is the best way to handle naming the items dynamically?</p>