How to dynamically assign property names for XmlOnlyMediaFormatter

Darryl Hoar 221 Reputation points
2026-08-26T15:38:37.4566667+00:00

I have been given an ASP.NET project to handle. It has been released and has worked fine.

I am not an ASP.NET developer but doing what I can. The issue:

The project is a webservice among other things. The solution (VS 2019) has 6 projects. One is the webservice.

The original developer created a class that has properties that have the same names as fields in a table in our sql server database. One user paid a company to create an app that through the web service pulls the data. This worked no problem. The fly in the ointment is that we changed the field names in the table as part of upgrades. This broke their custom app. They don't want to pay the developer to modify the app. I don't want to be bound by the customers external customizations. No, I can't say tough and force them.

The original developer used XmlOnlyMediaFormatter to return xml data for the webservice.

So, I was wondering if there was a way I can customize the xml tags returned from a webservice query with rewriting everything and changing the logic.

thanks for any guidance.

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


Answer accepted by question author
Tony Thach (WICLOUD CORPORATION) 760 Reputation points Microsoft External Staff Moderator
2026-08-27T02:49:54.2866667+00:00

Hi @Darryl Hoar , and thanks for posting your question.

The XML element names should be treated as part of the public API contract. Changing a SQL column or an internal C# property should not automatically change the XML consumed by existing clients.

The simplest solution depends on which serializer XmlOnlyMediaFormatter uses.

If it uses XmlSerializer

Keep the new internal property name and explicitly preserve the old XML element name:

using System.Xml.Serialization;
public class CustomerDto
{
    [XmlElement("FirstName")]

    public string GivenName { get; set; }

    [XmlElement("LastName")]

    public string Surname { get; set; }
}

The application can use GivenName and Surname, while the response remains compatible:

<CustomerDto>
  <FirstName>John</FirstName>
  <LastName>Smith</LastName>
</CustomerDto>

XmlElementAttribute controls the XML element name when the object is serialized by XmlSerializer. (xmlelementattribute)

If this is ASP.NET Web API 2 and the formatter is not already using XmlSerializer, it can be enabled in WebApiConfig.Register:

public static void Register(HttpConfiguration config)
{
    config.Formatters.XmlFormatter.UseXmlSerializer = true;
    // Other Web API configuration...
}

Test this change first because switching serializers globally can also affect root elements, namespaces, collections, null handling, and other existing responses. ASP.NET Web API supports configuring the XML serializer for the formatter or for individual types. (json-and-xml-serialization)

If it uses DataContractSerializer

Use DataContract and DataMember(Name = ...) instead:

using System.Runtime.Serialization;
[DataContract]

public class CustomerDto
{
    [DataMember(Name = "FirstName")]
    public string GivenName { get; set; }
    [DataMember(Name = "LastName")]
    public string Surname { get; set; }
}

Do not combine both approaches until you confirm which serializer the custom formatter actually invokes.

Recommended long-term design

Avoid returning database entity classes directly. Introduce a dedicated response DTO whose names represent the stable external contract:

public class CustomerResponseV1
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

Map the current database model to that DTO:

var response = new CustomerResponseV1
{
    FirstName = customer.GivenName,
    LastName = customer.Surname
};
return Ok(response);

This separates the layers:

Database model -> API DTO -> XML response

Future database changes can then be handled in the mapping without breaking clients.

Should the names be dynamic?

If “dynamic” means choosing different element names for different customers at runtime, attributes alone will not provide that behavior. Possible implementations include:

  1. Separate DTOs or API versions for each supported contract.
  2. A per-type XmlSerializer configured with XmlAttributeOverrides.
  3. A custom MediaTypeFormatter.

ASP.NET Web API supports custom media formatters, but one should only be introduced when normal DTO mapping cannot represent the required contract. (media-formatters)

For this scenario, a custom formatter is probably unnecessary. Preserve the old XML contract using [XmlElement] or a dedicated legacy DTO, then version the endpoint if you want to expose the new names later. Also add an integration test that compares the generated XML structure against the legacy schema before deploying the change.

If this instruction is applicable to your situation, I would greatly appreciate it if you could follow the instruction here so others experiencing similar behavior can benefit from it as well.  

Was this answer helpful?

5 people found this answer helpful.
0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 85,201 Reputation points
    2026-08-26T15:42:14.4+00:00

    you can override the field names in the class def:

        [XmlElement("FirstName")]
        public string First { get; set; } // Output tag will be <FirstName>
    
    

    if you really need dynamic, then your options are:

    • write your own xml formatter
    • create a new class(es) with the proper names and copy the data. the new class can be dynamically created via reflection.

    Was this answer helpful?

    2 people found this answer helpful.
    0 comments No comments

  2. Tony Thach (WICLOUD CORPORATION) 760 Reputation points Microsoft External Staff Moderator
    2026-08-27T02:27:59.97+00:00

    We are actively investigating this issue and will share updates as soon as they become available. Thank you for your patience and understanding.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.