title: C# - XML serialization - keep property value as it is
body: <p>I receive an object from another application and need to serialize it into XML. The object contains several properties and one of them contains already serialized part of XML.</p>
  
  <p>How can I keep it as it is during serialization to XML?</p>
  
  <p>Currently I am using XmlSerializer, but the problem is that it escapes some characters (&lt; and >) and serializes property as a value of XML node.</p>
  
  <p>Example, object structure:</p>
  
  <pre><code>public class SomeObject
  {
      public string A{get;set;}
      public string B {get;set;}
  
      //contains part of XML
      public string C {get;set;}
  }
  </code></pre>
  
  <p>Instance:</p>
  
  <pre><code>var a = new SomeObject
  {
      A = "A",
      B = "B",
      C = "&lt;C1&gt;test&lt;/C1&gt;&lt;C2&gt;test 2&lt;/C2&gt;&lt;C3&gt;test 2&lt;/C3&gt;"
  };
  </code></pre>
  
  <p>I need to keep C value as it is:</p>
  
  <pre><code>&lt;SomeObject&gt;
    &lt;A&gt;A&lt;/A&gt;
    &lt;B&gt;B&lt;/B&gt;
    &lt;C&gt;&lt;C1&gt;test&lt;/C1&gt;&lt;C2&gt;test 2&lt;/C2&gt;&lt;C3&gt;test 2&lt;/C3&gt;&lt;/C&gt;
  &lt;/SomeObject&gt;
  </code></pre>
  
  <p>Instead, the result looks differently (which is expected, but not what I need):</p>
  
  <pre><code>&lt;SomeObject&gt;
    &lt;A&gt;A&lt;/A&gt;
    &lt;B&gt;B&lt;/B&gt;
    &lt;C&gt;&amp;lt;C1&amp;gt;test&amp;lt;/C1&amp;gt;&amp;lt;C2&amp;gt;test 2&amp;lt;/C2&amp;gt;&amp;lt;C3&amp;gt;test 2&amp;lt;/C3&amp;gt;&lt;/C&gt;
  &lt;/SomeObject&gt;
  </code></pre>
  
  <p>Code that I am using for serialization:</p>
  
  <pre><code> public string Serialize(object obj)
          {
                  XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                  ns.Add("", "");
                  var xmlserializer = new XmlSerializer(obj.GetType(), "");
  
                  var stringWriter = new StringWriter();
                  var settings = new XmlWriterSettings { Indent = true, Encoding = Encoding.UTF8, OmitXmlDeclaration = true };
  
                  using (var writer = XmlWriter.Create(stringWriter, settings))
                  {
                      xmlserializer.Serialize(writer, obj,ns);
                      return stringWriter.ToString();
                  }
          }
  </code></pre>
  
  <p>Of course, I can create another object where C will be type of class and deserialize my C to that object, and then serialize that object. It will be serialized to the correct object, exactly what I need. But I want to avoid all those additional serialization/deserialization operations.</p>
  
  <p>Is there a way to specify for the serializer that the property should not be additionally serialized?</p>