title: C# Serialize a class with a List data member
body: <p>I have this c# class:</p>
  
  <pre><code>public class Test
  {
      public Test() { }
  
      public IList&lt;int&gt; list = new List&lt;int&gt;();
  }
  </code></pre>
  
  <p>Then I have this code:</p>
  
  <pre><code>        Test t = new Test();
          t.list.Add(1);
          t.list.Add(2);
  
          IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
          StringWriter sw = new StringWriter();
          XmlSerializer xml = new XmlSerializer(t.GetType());
          xml.Serialize(sw, t);
  </code></pre>
  
  <p>When I look at the output from sw, its this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-16"?&gt;
  &lt;Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" /&gt;
  </code></pre>
  
  <p>the values 1,2 I added to the list member variable dont show up.</p>
  
  <ol>
  <li>So how can I fix this ? I made the list a property but it still doesnt seem to work.</li>
  <li>I am using xml serialization here, are there any other serializers ?</li>
  <li>I want performance! Is this the best approach ?</li>
  </ol>
  
  <p>--------------- <strong>UPDATE BELOW</strong> -------------------------</p>
  
  <p>So the actual class I want to serialize is this:</p>
  
  <pre><code>public class RoutingResult
      {
          public float lengthInMeters { get; set; }
          public float durationInSeconds { get; set; }
  
          public string Name { get; set; }
  
          public double travelTime
          {
              get
              {
                  TimeSpan timeSpan = TimeSpan.FromSeconds(durationInSeconds);
                  return timeSpan.TotalMinutes;
              }
          }
  
          public float totalWalkingDistance
          {
              get
              {
                  float totalWalkingLengthInMeters = 0;
                  foreach (RoutingLeg leg in Legs)
                  {
                      if (leg.type == RoutingLeg.TransportType.Walk)
                      {
                          totalWalkingLengthInMeters += leg.lengthInMeters;
                      }
                  }
  
                  return (float)(totalWalkingLengthInMeters / 1000);
              }
          }
  
          public IList&lt;RoutingLeg&gt; Legs { get; set; } // this is a property! isnit it?
          public IList&lt;int&gt; test{get;set;} // test ...
  
          public RoutingResult()
          {
              Legs = new List&lt;RoutingLeg&gt;();
              test = new List&lt;int&gt;(); //test
              test.Add(1);
              test.Add(2);
              Name = new Random().Next().ToString(); // for test
          }
      }
  </code></pre>
  
  <p>But the XML produced by the serializer is this:</p>
  
  <pre><code>&lt;RoutingResult&gt;
    &lt;lengthInMeters&gt;9800.118&lt;/lengthInMeters&gt;
    &lt;durationInSeconds&gt;1440&lt;/durationInSeconds&gt;
    &lt;Name&gt;630104750&lt;/Name&gt;
  &lt;/RoutingResult&gt;
  </code></pre>
  
  <p>???</p>
  
  <p>its ignoring both of those lists ?</p>