title: How to serialize object in XML while flattening the internal hierarchy of properties within
body: <p>I am serializing an object using <a href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(v=VS.90).aspx" rel="nofollow">XmlSerializer</a>, specifying Attributes within my object to give instructions to the XmlSerializer, like so:</p>
  
  <pre><code>[XmlType("FooElement")]
  public class Foo {
  
      [XmlAttribute("Type")]
      public string fooType;
  
      [XmlElement("Name")]
      public string Name;
  
      [XmlElement("Bar")]
      public Bar BarObject;
  }
  
  public class Bar {
      [XmlElement("Message")]
      public string BarString;
      [XmlElement("From")]
      public string FromName;
  }
  </code></pre>
  
  <p>When an object of type Foo is serialized, the following XML is generated:</p>
  
  <pre><code>&lt;Foo Type="TypeName"&gt;
    &lt;Name&gt;Name of Foo&lt;/Name&gt;
    &lt;Bar&gt;
       &lt;Message&gt;5:00 Somewhere&lt;/Message&gt;
       &lt;From&gt;Jane Smith&lt;/From&gt;
    &lt;/Bar&gt;
  &lt;/Foo&gt;
  </code></pre>
  
  <p>What I want is for all data within Foo to be flattened to the same level, like so:</p>
  
  <pre><code>&lt;Foo Type="TypeName"&gt;
    &lt;Name&gt;Name of Foo&lt;/Name&gt;
    &lt;Message&gt;5:00 Somewhere&lt;/Message&gt;
    &lt;From&gt;Jane Smith&lt;/From&gt;
  &lt;/Foo&gt;
  </code></pre>
  
  <p>It is important for my needs that Foo and Bar remain separate classes. Aside from merging classes, How would I go about flattening the elements of Foo into a single level when serialized as XML? It would be best if it could be done through use of [Property] tags. I am open to switching my method of XML Serialization.</p>
  
  <p>Edit: I have switched to the <a href="http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx" rel="nofollow">XmlWriter</a> class. This will let me write XML elements in the hierarchy that I am looking for. The program consuming XML produced here is much simpler than the C# XML tools. The simpler parser was having difficulty with XmlSerializer generated XML.</p>