title: Serialize all properties in a class as attributes instead of elements
body: <p>I'm using XmlSerializer to serialize some classes to a XML File. Suppose I have this class: </p>
  
  <pre><code>public class ClassToSerialize
  {
      public string PropertyA { get; set; }
      public string PropertyB { get; set; }
      public string PropertyC { get; set; }
  }
  </code></pre>
  
  <p>if I serialize this class as it is, i'll get: </p>
  
  <pre><code>&lt;ClassToSerialize&gt;
      &lt;PropertyA&gt;{Value}&lt;/PropertyA}
      &lt;PropertyB&gt;{Value}&lt;/PropertyB}
      &lt;PropertyC&gt;{Value}&lt;/PropertyC}
  &lt;/ClassToSerialize&gt;
  </code></pre>
  
  <p>I'd like the properties to be serialized as xml attributes, instead of elements. I know i can achieve this by using [XmlAttribute] in each property:</p>
  
  <pre><code>public class ClassToSerialize
  {
      [XmlAttribute]
      public string PropertyA { get; set; }
  
      [XmlAttribute]
      public string PropertyB { get; set; }
  
      [XmlAttribute]
      public string PropertyC { get; set; }
  }
  </code></pre>
  
  <p>But i have a lot of classes, with a lot of properties. Is there any way I can set this option in the class level, or event better, in my XmlSerializer class?</p>
  
  <hr>
  
  <p>Based on the response given by @ulugbek-umirov, i create the following code to apply the XmlAttribute attribute to all properties in my class and base classes, in case anyone else needs it. This code is specific to my classes, because it only works with classes that starts with 'x', but If you need to adapt to your case it will easy.</p>
  
  <pre><code>private static void GenerateXmlAttributeOverrides(XmlAttributeOverrides overrides, Type type)
  {
      foreach (PropertyInfo propertyInfo in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
          {
              if ((propertyInfo.PropertyType.IsPrimitive || propertyInfo.PropertyType == typeof(string)))
              {
                  if (!(propertyInfo.Name.EndsWith("Specified")
                      || HasAttribute(propertyInfo, typeof(XmlElementAttribute))
                      || HasAttribute(propertyInfo, typeof(XmlAttributeAttribute))))
                  {
                      overrides.Add(type, propertyInfo.Name, new XmlAttributes { XmlAttribute = new XmlAttributeAttribute() });
                  }
              }
              else if (propertyInfo.PropertyType.IsGenericType)
              {
                  Type[] tipos = propertyInfo.PropertyType.GetGenericArguments();
                  if (tipos != null &amp;&amp; tipos.Length &gt; 0 &amp;&amp; tipos[0].Name.StartsWith("x"))
                      GenerateXmlAttributeOverrides(overrides, tipos[0]);
              }
              else if (propertyInfo.PropertyType.Name.StartsWith("x")) 
              {
                  GenerateXmlAttributeOverrides(overrides, propertyInfo.PropertyType);
              }                
          }
  
      }
  </code></pre>