title: C# XmlSerializer trim whitespaces
body: <p>I've trouble trimming strings when using the <a href="https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(v=vs.110).aspx" rel="nofollow"><code>XmlSerializer</code></a>, when using a XmlReader. The IgnoreWhitespace option shows no effect and the element string still contains \n and whitespaces.</p>
  
  <p>Is there any way to trim it "on the fly"?
  I would prefer such a method since I deserialize into an array of strings</p>
  
  <p>Here's an example xml:    </p>
  
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8'?&gt;
  &lt;root&gt;
    &lt;element&gt;
      some random string
    &lt;/element&gt;
    &lt;element&gt;
      another random string
    &lt;/element&gt;
  &lt;/root&gt;"
  </code></pre>
  
  <p>And here's the example code:</p>
  
  <pre><code>class Program
      {
          public static void Main(string[] args)
          {
              string xml =
              @"&lt;?xml version='1.0' encoding='UTF-8'?&gt;
              &lt;root&gt;
                &lt;element&gt;
                   some random string
                &lt;/element&gt;
                  &lt;element&gt;
                   another random string
                &lt;/element&gt;
              &lt;/root&gt;";
  
              var string_reader = new StringReader(xml);
  
              var xml_reader = XmlReader.Create(string_reader, new XmlReaderSettings()
              {
                  IgnoreWhitespace = true //setting this option doesn't work
              });
              root d = (root)new XmlSerializer(typeof(root)).Deserialize(xml_reader);
              d.elements[0].Contains("\n"); //true
  
              //Dispose string/xml reader
  
              Console.Read();
          }
      }
  
      public class root
      { 
          [XmlElement(ElementName ="element")]
          public string[] elements { set; get; }
      }
  </code></pre>