none
How do I prevent '&' to convert in '&amp' while xml serialization in ASP.NET Web API? RRS feed

  • Question

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

    For which response Model is something 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; }
    }

    Response must be of XML format, so I have serialized as of 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 receive response as of below, in which "&" changes 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 to avoid this change in my response ?

    Thanks In advance.

    Thursday, June 25, 2015 12:30 PM

Answers

  • Hi Richa Ankit,

    According this case, in my opinion is that this is correct behaviour.You want your response must be of XML format and "&amp" changes to "&",I thought it is mutually contradictory requirement. Because,In XML, some characters have special meaning.For example,If you put the character "<" in the XML element, an error will occur, this is because the parser would treat it as a new element start,like this:

    < message>if salary < 1000 then</message>

    To avoid this error, we use entity references instead of "<" characters,like this:

    < message>if salary &lt; 1000 then</message>

    "&" is the same , also need entity references "&amp" to instead.

    I hope that will be helpful to you.

    Best Regards,

    Tracy Dj

    Tuesday, June 30, 2015 7:34 AM
    Moderator

All replies

  • http://forums.asp.net/1246.aspx/1?Web+API

    The above forum is where you most likely need to post.

    Thursday, June 25, 2015 4:09 PM
  • Hi Richa Ankit,

    According this case, in my opinion is that this is correct behaviour.You want your response must be of XML format and "&amp" changes to "&",I thought it is mutually contradictory requirement. Because,In XML, some characters have special meaning.For example,If you put the character "<" in the XML element, an error will occur, this is because the parser would treat it as a new element start,like this:

    < message>if salary < 1000 then</message>

    To avoid this error, we use entity references instead of "<" characters,like this:

    < message>if salary &lt; 1000 then</message>

    "&" is the same , also need entity references "&amp" to instead.

    I hope that will be helpful to you.

    Best Regards,

    Tracy Dj

    Tuesday, June 30, 2015 7:34 AM
    Moderator
  • Hi Tracy

    Thanks for your reply, According to your reply Now I understand that while desalinizing the same data will return me the exact output. So there is no error in my code as well as in my response.

    Thanks a lot!!

    Regards,

    Richa 

    Monday, July 6, 2015 7:02 AM
  • Hi Richa,

    That's correct.


    Best Regards, Tracy Dj

    Tuesday, July 14, 2015 2:57 AM
    Moderator
  • Hi Richa 

    I'm getting & as string in double quotes so in this case it should not change. 

    Is it possible to handle such kind of values?

    Thanks in advance

    Ashish Trivedi

    Tuesday, April 17, 2018 1:24 PM