title: How do you (de)serialize a list of strings as CDATA using XmlSerializer
body: <p>I need to serialize a list of strings as CDATA and thought I would follow an <a href="https://stackoverflow.com/a/13801704/1805759">answer</a> of <a href="https://stackoverflow.com/questions/1379888/how-do-you-serialize-a-string-as-cdata-using-xmlserializer">How do you serialize a string as CDATA using XmlSerializer</a>.</p>
  
  <p>It works like a charm for serializing. My XML file looks as desired:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
   &lt;tlist&gt;
     &lt;item&gt;&lt;![CDATA[First string]]&gt;&lt;/item&gt;
     &lt;item&gt;&lt;![CDATA[Second string]]&gt;&lt;/item&gt;
   &lt;/tlist&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>But deserialization does not work. The TestList remains empty; value in the setter has count 0. What have I missed?</p>
  
  <pre><code>[XmlRootAttribute("root")]
  public class TestConfig
  {
    public TestConfig()
    {
      TestList = new List&lt;string&gt;();
      CdataList = new List&lt;XmlCDataSection&gt;();
    }
  
    [XmlIgnore]
    public List&lt;string&gt; TestList { get; set; }
  
    [XmlArray("tlist")]
    [XmlArrayItem("item")]
    public List&lt;XmlCDataSection&gt; CdataList
    {
      get { return TestList.Select(a =&gt; new XmlDocument().CreateCDataSection(a)).ToList(); }
      set
      {
        TestList = value.Select(s =&gt; s.Value).ToList();
      }
    }
  
    public void Save(string path)
    {
      var serializer = new XmlSerializer(GetType());
      using (var stream = new StreamWriter(path))
      {
        serializer.Serialize(stream, this);
      }
    }
  
    public static TestConfig Load(string path)
    {
      var serializer = new XmlSerializer(typeof(TestConfig));
      using (var stream = new StreamReader(path))
      {
        return (TestConfig)serializer.Deserialize(stream);
      }
    }
  }
  </code></pre>
  
  <p>Executing:</p>
  
  <pre><code>  var t = new TestConfig();
    t.TestList.Add("First string");
    t.TestList.Add("Second string");
    t.Save(@"C:\Test\cdatatest.xml");
  
    var r = TestConfig.Load(@"C:\Test\cdatatest.xml");
    Console.WriteLine("Testlist size is {0}", r.TestList.Count);
  </code></pre>