title: C# Deserialize inner XML to string
body: <p>I have the following XML:</p>
  
  <pre><code>&lt;MyType&gt;
    &lt;MyProperty1&gt;Value 1&lt;/MyProperty1&gt;
    &lt;MyProperty2&gt;Value 2&lt;/MyProperty2&gt;
    &lt;MyNestedXml&gt;
      &lt;h1&gt;Heading&lt;/h1&gt;
      &lt;p&gt;Lorum&lt;/p&gt;
      &lt;p&gt;Ipsum&lt;/p&gt;
    &lt;/MyNestedXml&gt;
  &lt;/MyType&gt;
  </code></pre>
  
  <p>This can be deserialized to XML in C# by creating classes and adding attributes as below:</p>
  
  <pre><code>[Serializable]
  public class MyType
  {
      [XmlElement("MyProperty1")
      public string MyProperty1 { get; set; }
  
      [XmlElement("MyProperty2")
      public string MyProperty2 { get; set; }
  
      [XmlIgnore]
      public string MyNestedXml { get; set; }
  }
  </code></pre>
  
  <p>However, the inner XML within the <code>&lt;MyNestedXml&gt;</code> element varies and doesn't follow a consistent structure which I can effectively map using attributes.</p>
  
  <p>I don't have control over the XML structure unfortunately.</p>
  
  <p>I have tried using an [XmlElement("MyNestedXml") with an XmlNode type but this results in the first child node being deserialized instead of the entire inner XML.</p>
  
  <p>I have also tried deserializing to a type of string but that throws an InvalidOperationException:</p>
  
  <p>"Unexpected node type Element. ReadElementString method can only be called on elements with simple or empty content."</p>
  
  <p>The problem is that the content of the MyNestedXml element could be an array of Elements sometimes but could be simple or empty content at other times.</p>
  
  <p>Ideally I could use a different serialization attribute such as [XmlAsString] to skip serialization altogether and just assign the inner XML as is.</p>
  
  <p>The intended result would be a class of type MyType having a property MyProperty1 = "<code>Value 1</code>", a property MyProperty2 = "<code>Value 2</code>", and a property MyNestedXml = "<code>&lt;h1&gt;Heading&lt;/h1&gt;&lt;p&gt;Lorum&lt;/p&gt;&lt;p&gt;Ipsum&lt;/p&gt;</code>".</p>