title: Serialize an Enumeration to an XML Attribute
body: <p>I'm generating >100 classes from a lot of XSD's to build our test harness application for internal testing. I'm using XSD.exe to generate these classes, as doing it by bring in too much risk and it would take way too long.</p>
  
  <p>I'm having an issue that is causing me a bit of pain. I'm trying to get this enumeration (for now just this one, then I'll do the same technique on the remaining ones...)</p>
  
  <p>What I would like is for this to generate something like this(where the receiver element has a typeCode attribute):</p>
  
  <pre><code>&lt;receiver typeCode="RCV"&gt;
      &lt;device classCode="DEV" determinerCode="INSTANCE"&gt;
          &lt;id root="..." extension="..." /&gt;
          &lt;asAgent classCode="AGNT"&gt;
              &lt;representedOrganization classCode="ORG" determinerCode="INSTANCE"&gt;
                  &lt;id root="..." extension="..." /&gt;
              &lt;/representedOrganization&gt;
          &lt;/asAgent&gt;
      &lt;/device&gt;
  &lt;/receiver&gt;
  </code></pre>
  
  <p>however, receiver never gets an attribute, even though I have explicetly set in the test harness startup object:</p>
  
  <pre><code>   this.receiver = new MCCI_MT000100BCReceiver();
     this.receiver.typeCode = CommunicationFunctionType.RSP;
     this.receiver.device = new MCCI_MT000100BCDevice();
  </code></pre>
  
  <p>I have an enumeration (I've added the XMLEnums and the [Flags] attribute as XSD.exe didn't bother)</p>
  
  <pre><code>[Flags]
  [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
  [System.SerializableAttribute()]
  [System.Xml.Serialization.XmlTypeAttribute(Namespace = "urn:hl7-org:v3")]
  public enum CommunicationFunctionType
  {
  
      /// &lt;remarks/&gt;
      [System.Xml.Serialization.XmlEnum("RCV")]
      RCV = 1,
  
      /// &lt;remarks/&gt;
      [System.Xml.Serialization.XmlEnum("RSP")]
      RSP = 2,
  
      /// &lt;remarks/&gt;
      [System.Xml.Serialization.XmlEnum("SND")]
      SND = 3,
  }
  </code></pre>
  
  <p>And this is one of the classes that use that enum:</p>
  
  <pre><code>[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
      [System.SerializableAttribute()]
      [System.Diagnostics.DebuggerStepThroughAttribute()]
      [System.ComponentModel.DesignerCategoryAttribute("code")]
      [System.Xml.Serialization.XmlTypeAttribute(TypeName = "MCCI_MT000100BC.Receiver", Namespace = "urn:hl7-org:v3")]
      public partial class MCCI_MT000100BCReceiver
      {
  
          private CS[] realmCodeField;
  
          private II typeIdField;
  
          private II[] templateIdField;
  
          private MCCI_MT000100BCDevice deviceField;
  
          private CommunicationFunctionType typeCodeField;
  
          private bool typeCodeFieldSpecified;
  
          public MCCI_MT000100BCReceiver()
          {
              this.typeCodeField = CommunicationFunctionType.RCV;
          }
  
          /// &lt;remarks/&gt;
          [System.Xml.Serialization.XmlElementAttribute("realmCode")]
          public CS[] realmCode
          {
              get
              {
                  return this.realmCodeField;
              }
              set
              {
                  this.realmCodeField = value;
              }
          }
  
          /// &lt;remarks/&gt;
          public II typeId
          {
              get
              {
                  return this.typeIdField;
              }
              set
              {
                  this.typeIdField = value;
              }
          }
  
          /// &lt;remarks/&gt;
          [System.Xml.Serialization.XmlElementAttribute("templateId")]
          public II[] templateId
          {
              get
              {
                  return this.templateIdField;
              }
              set
              {
                  this.templateIdField = value;
              }
          }
  
          /// &lt;remarks/&gt;
          public MCCI_MT000100BCDevice device
          {
              get
              {
                  return this.deviceField;
              }
              set
              {
                  this.deviceField = value;
              }
          }
  
          /// &lt;remarks/&gt;
  
          [System.Xml.Serialization.XmlAttributeAttribute()]
          public CommunicationFunctionType typeCode
          {
              get
              {
                  return this.typeCodeField;
              }
              set
              {
                  this.typeCodeField = value;
              }
          }
  
          /// &lt;remarks/&gt;
          [System.Xml.Serialization.XmlIgnoreAttribute()]
          public bool typeCodeSpecified
          {
              get
              {
                  return this.typeCodeFieldSpecified;
              }
              set
              {
                  this.typeCodeFieldSpecified = value;
              }
          }
      }
  </code></pre>
  
  <p>I understand that serializing enumerations isn't something .NET likes to do at times, but any help would be appreciated.</p>