Convert generic class into XML string of specific structure There is a need to have a specific structure of XML string when interacting with 3rd party system. Here are two examples:

&lt;DocumentElement&gt;
 &lt;Country&gt;
  &lt;CountryCode&gt;UA&lt;/CountryCode&gt;
  &lt;Name&gt;Ukraine&lt;/Name&gt;
 &lt;/Country&gt;
&lt;/DocumentElement&gt;


and

&lt;DocumentElement&gt;
 &lt;Currency&gt;
  &lt;CurrencyCode&gt;UAH&lt;/CurrencyCode&gt;
  &lt;Description&gt;Hryvnia&lt;/Description&gt;
 &lt;/Currency&gt;
&lt;/DocumentElement&gt;


Like you can see it is a common wrapping tag DocumentElement.
So, here is approach #1:

Have a wrapper class:

[XmlRoot("DocumentElement")]
public class DocumentElement&lt;T&gt;
{
    public T InnerObject { get; set; }

    public DocumentElement(){}
    public DocumentElement(T some)
    {
        InnerObject = some;
    }
}


And replace InnerObject after conversion:

public static string GetXml&lt;T&gt;(T instance) where T : class
{
    var wrapper = new DocumentElement&lt;T&gt;(instance);
    var writer = new StringWriter();
    var serializer = new XmlSerializer(typeof(DocumentElement&lt;T&gt;));
    serializer.Serialize(writer, wrapper);
    return writer.ToString().Replace("InnerObject", instance.GetType().Name);
}


Or, approach #2:

Throw away DocumentElement and just concat a needed string:

public static string GetXml&lt;T&gt;(T instance) where T : class
{
    var writer = new StringWriter();
    var serializer = new XmlSerializer(typeof(T));
    serializer.Serialize(writer, instance);
    return "&lt;DocumentElement&gt;" + writer.ToString() + "&lt;/DocumentElement&gt;";
}


Both approaches produce a needed format but neither looks clean enough.
Maybe, someone can suggest a better approach to this?
Take a look at  How to: Specify an Alternate Element Name for an XML Stream. 
Although it is more code, I would use the mentioned XmlAttributeOverrides like so

public static string GetXml&lt;T&gt;(T instance, string sourceName = "InnerObject") where T : class
{
    var wrapper = new DocumentElement&lt;T&gt;(instance);
    var writer = new StringWriter();
    var overrides = CreateOverrides(typeof(DocumentElement&lt;T&gt;), instance.GetType().Name, sourceName);

    var serializer = new XmlSerializer(typeof(DocumentElement&lt;T&gt;), overrides);
    serializer.Serialize(writer, wrapper);
    return writer.ToString();
}
private static XmlAttributeOverrides CreateOverrides(Type type, string destinationName, string sourceName)
{
    var elementAttribute = new XmlElementAttribute() { ElementName = destinationName };
    var attributes = new XmlAttributes();
    attributes.XmlElements.Add(elementAttribute);
    var overrides = new XmlAttributeOverrides();
    overrides.Add(type, sourceName, attributes);
    return overrides;
}