How to prevent "&" from being converted in "&" when doing xml serialization in ASP.NET Web API?


Rika Anchi

I am creating a booking service using ASP.NET Web API,

For which response model it looks like this:

[XmlRoot("RateList")]
public class RateList
{
    [XmlElement("Rate")]
    public List<Rate> Rate { get; set; }

}
public class Rate
{
    [XmlAttribute("Code")]
    public string Code { get; set; }
    [XmlAttribute("ErrorMessage")]
    public string ErrorMessage { get; set; }
     [XmlElement("RoomRate")]
    public List<RoomRate> RoomRate { get; set; }
}
public class RoomRate
{
    [XmlAttribute("URL")]
    public string URL { get; set; }
}

The response must be in XML format, so I have serialized from below,

return Request.CreateResponse<RateList>(HttpStatusCode.OK, objRateList, Configuration.Formatters.XmlFormatter);

my Global.asax file

WebApiConfig.Register(GlobalConfiguration.Configuration);
        var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
        xml.UseXmlSerializer = true;

The actual response must be like this:

<RateList 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Rate Code="174511">
        <RoomRate URL="https://somedomain.com/planning/booking?start=2015-06-02&end=2015-06-04&id=174511&adults=2&childAges=0&room=1"/>
</Rate>
</RateList>

But currently, I get a response like the following, where the "&" is changed to " &amp;":

<RateList 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Rate Code="174511">
        <RoomRate URL="https://somedoamin.com/planning/booking?start=2015-06-02&amp;end=2015-06-04&amp;id=174511&amp;adults=2&amp;childAges=0&amp;room=1"/>
</Rate>
</RateList>

How can I avoid this change in my reply?

Thanks in advance.

Florence George

This is correct behavior. Without escaping, the XML is ill-formed (that is, the parser must report an error and cannot parse it). If you want to generate XML, you have to escape &characters.

The value of the property contains one &. The way to represent it lexically in XML is to write it as &amp;. If you parse the XML and ask for the attribute value as a string, you will see that the parser has decoded the attribute correctly.

You might consider using backslashes the same way you escape special characters in Java or C++ strings. String values ​​do not contain any backslashes, but you must use them to lexically represent some special characters.

Related


How to read XML from ASP.NET Web API?

not me I have a web API that reads XML and passes it to the appropriate model for processing. How to receive incoming XML? Which data type should I use? When is it used StreamReader, StreamContentor XmlDocumentis it something else? wake ASP.NET Web API uses co

How to read XML from ASP.NET Web API?

not me I have a web API that reads XML and passes it to the appropriate model for processing. How to receive incoming XML? Which data type should I use? When is it used StreamReader, StreamContentor XmlDocumentis it something else? wake ASP.NET Web API uses co

How to read XML from ASP.NET Web API?

not me I have a web API that reads XML and passes it to the appropriate model for processing. How to receive incoming XML? Which data type should I use? When is it used StreamReader, StreamContentor XmlDocumentis it something else? wake ASP.NET Web API uses co

How to enable BSON serialization in ASP.NET Core Web API?

alkene I am new to ASP.NET Core and web programming. I just successfully completed my first ASP.NET Core Web API based on RESTfull design principles. It's currently sending the response using JSON serialization (Visual Studio default), but I'd like to try BSON

How to enable BSON serialization in ASP.NET Core Web API?

alkene I am new to ASP.NET Core and web programming. I just successfully completed my first ASP.NET Core Web API based on RESTfull design principles. It's currently sending the response using JSON serialization (Visual Studio default), but I'd like to try BSON

How to enable BSON serialization in ASP.NET Core Web API?

alkene I am new to ASP.NET Core and web programming. I just successfully completed my first ASP.NET Core Web API based on RESTfull design principles. It's currently sending the response using JSON serialization (Visual Studio default), but I'd like to try BSON

How to enable BSON serialization in ASP.NET Core Web API?

alkene I am new to ASP.NET Core and web programming. I just successfully completed my first ASP.NET Core Web API based on RESTfull design principles. It's currently sending the response using JSON serialization (Visual Studio default), but I'd like to try BSON

How to enable BSON serialization in ASP.NET Core Web API?

alkene I am new to ASP.NET Core and web programming. I just successfully completed my first ASP.NET Core Web API based on RESTfull design principles. It's currently sending the response using JSON serialization (Visual Studio default), but I'd like to try BSON