title: XML Serialization of HTML
body: <p><strong>Okay this one DID it! Thanks to all of you!</strong></p>
  
  <pre><code>public class Result
  {
      public String htmlEscaped
      {
          set;
          get;
      }
  
      [XmlIgnore]
      public String htmlValue
      { set; get; }
  
      [XmlElement("htmlValue")]
      public XmlCDataSection htmlValueCData
      {
          get
          {
              XmlDocument _dummyDoc = new XmlDocument();
              return _dummyDoc.CreateCDataSection(htmlValue);
          }
          set { htmlValue = (value != null) ? value.Data : null; }
      }
  }
  
      Result r = new Result();
      r.htmlValue = ("&lt;b&gt;Hello&lt;/b&gt;");
      r.htmlEscaped = ("&lt;b&gt;Hello&lt;/b&gt;");
      XmlSerializer xml = new XmlSerializer(r.GetType());
      TextWriter file = new StreamWriter(Environment.CurrentDirectory + "\\results\\result.xml", false, System.Text.Encoding.Default);
      xml.Serialize(file, r);
      file.Close();
  </code></pre>
  
  <p>RESULT:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="Windows-1252"?&gt;
  &lt;Result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;htmlEscaped&gt;&amp;lt;b&amp;gt;Hello&amp;lt;/b&amp;gt;&lt;/htmlEscaped&gt;
    &lt;htmlValue&gt;&lt;![CDATA[&lt;b&gt;Hello&lt;/b&gt;]]&gt;&lt;/htmlValue&gt;
  &lt;/Result&gt;
  </code></pre>
  
  <p>As you can see, after CDATA is return type, no more escaped html in XML file on filesystem.
  The JSON Serialization isn't working anymore, but this can be fixed with a little type extention.</p>
  
  <p><hr>
  QUESTION WAS:</p>
  
  <p>Maybe someone knows how to make do it...</p>
  
  <p>I have this Class:</p>
  
  <pre><code>public class Result
  {
      public String htmlValue
      {
          get;
          set;
      }
  }
  </code></pre>
  
  <p>I use this to serialize it to XML</p>
  
  <pre><code>Result res = new Result();
  res.htmlValue = "&lt;p&gt;Hello World&lt;/p&gt;";
  XmlSerializer s = new XmlSerializer(res.GetType());
  TextWriter w = new StreamWriter(Environment.CurrentDirectory + "\\result.xml", false, System.Text.Encoding.Default);
  s.Serialize(w, res);
  w.Close();
  </code></pre>
  
  <p>Works fine i get this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="Windows-1252"?&gt;
  &lt;Result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;htmlValue&gt;&amp;lt;b&amp;gt;Hello World&amp;lt;/b&amp;gt;&lt;/htmlValue&gt;
  &lt;/Result&gt;
  </code></pre>
  
  <p>What can do i have to change to get this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="Windows-1252"?&gt;
  &lt;Result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;htmlValue&gt;&lt;![CDATA[&lt;b&gt;Hello World&lt;/b&gt;]]&gt;&lt;/htmlValue&gt;
  &lt;/Result&gt;
  </code></pre>
  
  <p>I've already searched but I can't find anything. The type of htmlValue
  have to stay String, because of other Serialisations JSON, etc.</p>
  
  <p>Tricky one... Thanks in advance for suggestions</p>
  
  <ul>
  <li>HTML is correct in String within C#. Why decode or encode?</li>
  <li>XmlSerializer saved the HTML escaped to XML file.</li>
  <li>Don't use C# for consuming. </li>
  </ul>
  
  <p>Is external tool which accept this:</p>
  
  <pre><code>&lt;htmlValue&gt;&lt;![CDATA[&lt;b&gt;Hello World&lt;/b&gt;]]&gt;&lt;/htmlValue&gt;
  </code></pre>
  
  <p>but not </p>
  
  <pre><code>&lt;htmlValue&gt;&amp;lt;b&amp;gt;Hello World&amp;lt;/b&amp;gt;&lt;/htmlValue&gt;
  </code></pre>
  
  <p>I do the same with JSON Serializer, in file on hard drive the HTML is saved correct.
  Why and where to use HTTP Utility to prevent that? And how to get <code>&lt;![CDATA[ ]]&gt;</code> around it.</p>
  
  <p>Can you give a code sample?
  Are there any other Serializer than the C# own one?</p>
  
  <p>I've found this Link <a href="http://blogit.create.pt/blogs/marcosilva/archive/2008/11/23/.NET-XML-Serialization-of-CDATA-ATTRIBUTE.aspx" rel="noreferrer">.NET XML Serialization of CDATA ATTRIBUTE </a> from Marco André Silva, which does I need to do, but it's different, how to include this without changing Types?</p>