title: Using XmlAttributeOverrides to change element names in object tree
body: <p>I need to figure out how to rename elements and/or attributes in generated XML using values retrieved from the database as my element names. </p>
  
  <p>For instance, here is a potential XML output from my current process:</p>
  
  <pre><code>&lt;ArrayOfEntityTreeBase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;EntityTreeBase EntityInfo="User"&gt;
      &lt;Children&gt;
        &lt;EntityTreeBase EntityInfo="User Medication"&gt;
          &lt;Properties&gt;
            &lt;EntityProperty CreatedDate="2013-11-14T16:41:12.75"&gt;
              &lt;FieldName&gt;Medication Name&lt;/FieldName&gt;
              &lt;Value&gt;Celebrex&lt;/Value&gt;
            &lt;/EntityProperty&gt;
            &lt;EntityProperty CreatedDate="2013-12-04T14:08:58.597"&gt;
              &lt;FieldName&gt;Medication Dosage&lt;/FieldName&gt;
              &lt;Value&gt;20000MG&lt;/Value&gt;
            &lt;/EntityProperty&gt;
            &lt;EntityProperty CreatedDate="2013-11-14T16:41:12.76"&gt;
              &lt;FieldName&gt;Medication Prescribed Date&lt;/FieldName&gt;
              &lt;Value&gt;08/01/2013&lt;/Value&gt;
            &lt;/EntityProperty&gt;
          &lt;/Properties&gt;
        &lt;/EntityTreeBase&gt;
        &lt;EntityTreeBase EntityInfo="User Medication"&gt;
          &lt;Properties&gt;
            &lt;EntityProperty CreatedDate="2013-11-14T16:41:12.767"&gt;
              &lt;FieldName&gt;Medication Name&lt;/FieldName&gt;
              &lt;Value&gt;Aspirin&lt;/Value&gt;
            &lt;/EntityProperty&gt;
            &lt;EntityProperty CreatedDate="2013-11-14T16:41:12.77"&gt;
              &lt;FieldName&gt;Medication Dosage&lt;/FieldName&gt;
              &lt;Value&gt;5 mg&lt;/Value&gt;
            &lt;/EntityProperty&gt;
            &lt;EntityProperty CreatedDate="2013-11-14T16:41:12.78"&gt;
              &lt;FieldName&gt;Medication Prescribed Date&lt;/FieldName&gt;
              &lt;Value&gt;09/01/2013&lt;/Value&gt;
            &lt;/EntityProperty&gt;
          &lt;/Properties&gt;
        &lt;/EntityTreeBase&gt;
        &lt;EntityTreeBase EntityInfo="User Medication"&gt;
          &lt;Properties&gt;
            &lt;EntityProperty CreatedDate="2013-11-14T16:41:12.783"&gt;
              &lt;FieldName&gt;Medication Name&lt;/FieldName&gt;
              &lt;Value&gt;Celebrex&lt;/Value&gt;
            &lt;/EntityProperty&gt;
            &lt;EntityProperty CreatedDate="2013-11-14T16:41:12.793"&gt;
              &lt;FieldName&gt;Medication Dosage&lt;/FieldName&gt;
              &lt;Value&gt;50 mg twice a day&lt;/Value&gt;
            &lt;/EntityProperty&gt;
            &lt;EntityProperty CreatedDate="2013-11-14T16:41:12.8"&gt;
              &lt;FieldName&gt;Medication Prescribed Date&lt;/FieldName&gt;
              &lt;Value&gt;10/01/2013&lt;/Value&gt;
            &lt;/EntityProperty&gt;
          &lt;/Properties&gt;
        &lt;/EntityTreeBase&gt;
      &lt;/Children&gt;
      &lt;Properties&gt;
        &lt;EntityProperty CreatedDate="2013-12-03T13:48:03.45"&gt;
          &lt;FieldName&gt;User First Name&lt;/FieldName&gt;
          &lt;Value&gt;John&lt;/Value&gt;
        &lt;/EntityProperty&gt;
        &lt;EntityProperty CreatedDate="2013-12-03T11:36:31.423"&gt;
          &lt;FieldName&gt;User MI&lt;/FieldName&gt;
          &lt;Value&gt;Q&lt;/Value&gt;
        &lt;/EntityProperty&gt;
        &lt;EntityProperty CreatedDate="2013-11-19T09:56:44.66"&gt;
          &lt;FieldName&gt;User Last Name&lt;/FieldName&gt;
          &lt;Value&gt;Public&lt;/Value&gt;
        &lt;/EntityProperty&gt;
        &lt;EntityProperty CreatedDate="2013-11-14T16:41:12.803"&gt;
          &lt;FieldName&gt;User SSN&lt;/FieldName&gt;
          &lt;Value&gt;111-22-3333&lt;/Value&gt;
        &lt;/EntityProperty&gt;
      &lt;/Properties&gt;
    &lt;/EntityTreeBase&gt;
  &lt;/ArrayOfEntityTreeBase&gt;
  </code></pre>
  
  <p>What I need to accomplish is this:</p>
  
  <pre><code>&lt;UserInformation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;User&gt;
      &lt;UserMedications&gt;
        &lt;UserMedication&gt;
          &lt;MedicationProperties&gt;
            &lt;MedicationProperty CreatedDate="2013-11-14T16:41:12.75"&gt;
              &lt;FieldName&gt;Medication Name&lt;/FieldName&gt;
              &lt;Value&gt;Celebrex&lt;/Value&gt;
            &lt;/MedicationProperty&gt;
            &lt;MedicationProperty CreatedDate="2013-12-04T14:08:58.597"&gt;
              &lt;FieldName&gt;Medication Dosage&lt;/FieldName&gt;
              &lt;Value&gt;20000MG&lt;/Value&gt;
            &lt;/MedicationProperty&gt;
            &lt;MedicationProperty CreatedDate="2013-11-14T16:41:12.76"&gt;
              &lt;FieldName&gt;Medication Prescribed Date&lt;/FieldName&gt;
              &lt;Value&gt;08/01/2013&lt;/Value&gt;
            &lt;/MedicationProperty&gt;
          &lt;/MedicationProperties&gt;
        &lt;/UserMedication&gt;
        &lt;UserMedication&gt;
          &lt;MedicationProperties&gt;
            &lt;MedicationProperty CreatedDate="2013-11-14T16:41:12.767"&gt;
              &lt;FieldName&gt;Medication Name&lt;/FieldName&gt;
              &lt;Value&gt;Aspirin&lt;/Value&gt;
            &lt;/MedicationProperty&gt;
            &lt;MedicationProperty CreatedDate="2013-11-14T16:41:12.77"&gt;
              &lt;FieldName&gt;Medication Dosage&lt;/FieldName&gt;
              &lt;Value&gt;5 mg&lt;/Value&gt;
            &lt;/MedicationProperty&gt;
            &lt;MedicationProperty CreatedDate="2013-11-14T16:41:12.78"&gt;
              &lt;FieldName&gt;Medication Prescribed Date&lt;/FieldName&gt;
              &lt;Value&gt;09/01/2013&lt;/Value&gt;
            &lt;/MedicationProperty&gt;
          &lt;/MedicationProperties&gt;
        &lt;/UserMedication&gt;
        &lt;UserMedication&gt;
          &lt;MedicationProperties&gt;
            &lt;MedicationProperty CreatedDate="2013-11-14T16:41:12.783"&gt;
              &lt;FieldName&gt;Medication Name&lt;/FieldName&gt;
              &lt;Value&gt;Celebrex&lt;/Value&gt;
            &lt;/MedicationProperty&gt;
            &lt;MedicationProperty CreatedDate="2013-11-14T16:41:12.793"&gt;
              &lt;FieldName&gt;Medication Dosage&lt;/FieldName&gt;
              &lt;Value&gt;50 mg twice a day&lt;/Value&gt;
            &lt;/MedicationProperty&gt;
            &lt;MedicationProperty CreatedDate="2013-11-14T16:41:12.8"&gt;
              &lt;FieldName&gt;Medication Prescribed Date&lt;/FieldName&gt;
              &lt;Value&gt;10/01/2013&lt;/Value&gt;
            &lt;/MedicationProperty&gt;
          &lt;/MedicationProperties&gt;
        &lt;/UserMedication&gt;
      &lt;/UserMedications&gt;
      &lt;UserProperties&gt;
        &lt;UserProperty CreatedDate="2013-12-03T13:48:03.45"&gt;
          &lt;FieldName&gt;User First Name&lt;/FieldName&gt;
          &lt;Value&gt;John&lt;/Value&gt;
        &lt;/UserProperty&gt;
        &lt;UserProperty CreatedDate="2013-12-03T11:36:31.423"&gt;
          &lt;FieldName&gt;User MI&lt;/FieldName&gt;
          &lt;Value&gt;Q&lt;/Value&gt;
        &lt;/UserProperty&gt;
        &lt;UserProperty CreatedDate="2013-11-19T09:56:44.66"&gt;
          &lt;FieldName&gt;User Last Name&lt;/FieldName&gt;
          &lt;Value&gt;Public&lt;/Value&gt;
        &lt;/UserProperty&gt;
        &lt;UserProperty CreatedDate="2013-11-14T16:41:12.803"&gt;
          &lt;FieldName&gt;User SSN&lt;/FieldName&gt;
          &lt;Value&gt;111-22-3333&lt;/Value&gt;
        &lt;/UserProperty&gt;
      &lt;/UserProperties&gt;
    &lt;/User&gt;
  &lt;/UserInformation&gt;
  </code></pre>
  
  <p>Here are my objects:</p>
  
  <pre><code>public class EntityProperty
  {
      [XmlIgnore]
      public int FieldId { get; set; }
      public string FieldName { get; set; }
      [XmlIgnore]
      public int FieldSortOrder { get; set; }
      [XmlAttribute()]
      public DateTime CreatedDate { get; set; }
      [XmlIgnore]
      public bool IsIterative { get; set; }
      public string Value { get; set; }
      public EntityTreeBase Entity { get; set; }
      public EntityProperty() { }
      public EntityProperty(int fieldId, string fieldName, int fieldSortOrder, DateTime createdDate, bool isIterative, string valueIn)
      {
          FieldId = fieldId;
          FieldName = FieldName;
          FieldSortOrder = fieldSortOrder;
          CreatedDate = createdDate;
          IsIterative = isIterative;
          Value = valueIn;
      }
  }
  
  public class EntityTreeBase
  {
      [XmlIgnore]
      public long EntityId { get; set; }
      [XmlIgnore]
      public long? ParentEntityId { get; set; }
      [XmlIgnore]
      public int EntityDefinitionId { get; set; }
      [XmlIgnore]
      public int DestinationId { get; set; }
      [XmlIgnore]
      public int Level { get; set; }
      [XmlAttribute("EntityInfo")]
      public string EntityDefinitionName { get; set; }
      public EntityTreeBaseCollection Children { get; set; }
      public EntityPropertiesCollection Properties { get; set; }
      public EntityTreeBase() { }
      public EntityTreeBase(long entityId, long? parentEntityId, int entityDefinitionId, int destinationId, int level, string entityDefinitionName)
      {
          EntityId = entityId;
          ParentEntityId = parentEntityId;
          EntityDefinitionId = entityDefinitionId;
          DestinationId = destinationId;
          Level = level;
          EntityDefinitionName = entityDefinitionName;
      }
      public bool HasChildren
      {
          get { return (Children != null &amp;&amp; Children.Count &gt; 0); }
      }
      public bool HasProperties
      {
          get { return (Properties != null &amp;&amp; Properties.Count &gt; 0); }
      }
      public static EntityTreeBase BuildTree(EntityTreeBaseCollection collection, EntityTreeBase parent)
      {
          parent.Properties = EntityPropertiesCollection.GetProperties(parent.DestinationId, parent.EntityId, parent.EntityDefinitionId);
          parent.Children = new EntityTreeBaseCollection();
          foreach (EntityTreeBase item in EntityTreeBaseCollection.FindChildEntities(collection, parent.EntityId))
          {
              parent.Children.Add(BuildTree(EntityTreeBaseCollection.GetChildren(item.EntityId, item.Level, item.DestinationId), item));
          }
          if (!parent.HasChildren)
          {
              parent.Children = null;
          }
          if (!parent.HasProperties)
          {
              parent.Properties = null;
          }
          return parent;
      }
  }
  </code></pre>
  
  <p>So, as is hopefully obvious, there are no object types named "Medication" or "User", these must be inferred from the data.  So I need to know how to use values from the data to change my element names, but I need to figure out how to crawl the object tree so each element name is changed based on the associated EntityDefinitionName.  I'm using recursion to populate my object tree prior to serialization.  I know the following code works to rename my XmlRoot:</p>
  
  <pre><code>XmlAttributeOverrides xmlOverrides = new XmlAttributeOverrides();
  XmlAttributes attribs = new XmlAttributes();
  XmlRootAttribute rootAttr = new XmlRootAttribute();
  rootAttr.ElementName = collection.Find(e =&gt; e.Level == 1).EntityDefinitionName.Replace(" ", "");
  attribs.XmlRoot = rootAttr;
  </code></pre>
  
  <p>But I need to figure out how to change each element name based on the EntityDefinitionName associated with that element or node.</p>
  
  <p>Thanks in advance!</p>