How do I modify the root XML tag at runtime?



I'm new to XML having usually dealt in JSON for my APIs, so feel free to tell me if I'm trying to reinvent the wheel here.


Back story: I'm currently working on an integration that only allows me to have one endpoint to supports multiple requests, i.e. Search, Sell, Update, Cancel. They only support XML, so I cannot use JSON. I determine the type of request from the root XML name, do my work, and then send back a response.


Problem: As this is XML, I have to return a strongly-typed object for serialization which prevents me from using many custom classes with [XmlRoot(ElementName = "blah")]. Thus, I need to set the root element name at run time to support the different named replies I have to send.


My response class:



public class Response
{
public Errors Error { get; set; }
public RequestData Request { get; set; }
public List<Limo> Limos { get; set; }
public string ReservationID { get; set; }
public string Status { get; set; }
public string ConfNum { get; set; }
public string CancelPolicy { get; set; }
}


produces a response of



<?xml version="1.0" encoding="utf-16"?>
<Response xmlns:xsi="http://ift.tt/ra1lAU" xmlns:xsd="http://ift.tt/tphNwY">
<Error>
<ErrorCode />
<ErrorSource />
<ErrorDescription />
</Error>
<Request>
<ServiceType>300</ServiceType>
<StartDateTime>2015-09-30T09:00:00</StartDateTime>
<NumPassengers>1</NumPassengers>
<VehicleType>100</VehicleType>
</Request>
<Limos />
</Response>


Basically, I need to be able to change <Response xmlns:xsi="http://ift.tt/ra1lAU" xmlns:xsd="http://ift.tt/tphNwY"> into <SearchReply>, <SellReply>, <UpdateReply>, or <CancelReply> as needed to end up with something like this



<?xml version="1.0" encoding="utf-16"?>
<SearchReply>
<Error>
<ErrorCode />
<ErrorSource />
<ErrorDescription />
</Error>
<Request>
<ServiceType>300</ServiceType>
<StartDateTime>2015-09-30T09:00:00</StartDateTime>
<NumPassengers>1</NumPassengers>
<VehicleType>100</VehicleType>
</Request>
<Limos />
</SearchReply>

No comments:

Post a Comment