title: How do I serialize a list of objects with System.Runtime.Serialization.Json?
body: <p>I'm trying to avoid using any third party DLLs to turn some data to JSON.  </p>
  
  <p>Say I have the class:</p>
  
  <pre><code>public class NodeData {
  
    public int NodeID { get; set; }
    public int ParentID { get; set; }
    public int Index { get; set; }
    public string FriendlyName { get; set; }
  
    public NodeData(int nodeID, int parentID, int index, string friendlyName) {
      this.NodeID = nodeID;
      this.ParentID = parentID;
      this.Index = index;
      this.FriendlyName = friendlyName;
    }
  }
  </code></pre>
  
  <p>Then I use the <code>DataContractJsonSerializer</code> to turn a constructed list of <code>NodeData</code> objects to a JSON string like so:</p>
  
  <pre><code>MemoryStream ms = new MemoryStream();
  DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(List&lt;NodeData&gt;));
  ds.WriteObject(ms, data);
  
  string jsonString = Encoding.UTF8.GetString(ms.ToArray());
  ms.Close();
  </code></pre>
  
  <p>This throws a <code>System.Runtime.Serialization.InvalidDataContractException</code>:</p>
  
  <blockquote>
    <p>Type 'xxx.xxx.NodeData' cannot be serialized. Consider marking it
    with the DataContractAttribute attribute, and marking all of its
    members you want serialized with the DataMemberAttribute attribute. 
    If the type is a collection, consider marking it with the
    CollectionDataContractAttribute.  See the Microsoft .NET Framework
    documentation for other supported types</p>
  </blockquote>
  
  <p>How would I make my list valid for serialization to JSON with System.Runtime.Serialization.Json?</p>