title: How to serialize class members as attributes to xml in C#
body: <p>I am trying to serialize my object to xml.  A serializer seemingly serializes all data members as children, but I want to serialize all members as attributes, not children.</p>
  
  <p>Here's a code example:</p>
  
  <pre><code>[DataContract]
  public class MyDataClass
  {
    [DataMember]
    int foo = 24;
    [DataMember]
    string bar = "brabrabra";
  } 
  </code></pre>
  
  <p>This will be serialized as following xml when I use DataContractSerializer:</p>
  
  <pre><code>&lt;MyDataClass xmlns="..." xmlns:i="..."&gt;
    &lt;foo&gt;24&lt;/foo&gt;
    &lt;bar&gt;brabrabra&lt;/bar&gt;
  &lt;/MyDataClass&gt;
  </code></pre>
  
  <p>However, I want to serialize it as following xml somehow:</p>
  
  <pre><code>&lt;MyDataClass xmlns="..." xmlns:i="..." foo="24" bar="brabrabra" /&gt;
  </code></pre>
  
  <p>Is there any way to serialize like that?  Or, should I write my own serializer to realize it?
  For reference, I am using DataContract serializer in this sample, but I can change it to a normal XmlSerializer or another one if there's a better one.</p>
  
  <p>Hope someone knows about this.</p>
  
  <p>Aki</p>