How do I prevent '&' to convert in '&' while xml serialization in ASP.NET Web API? -
i creating booking service using asp.net web api,
for response model 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 of xml format, have serialized 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 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 receive response of below, in "&" changes "&
":
<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&end=2015-06-04&id=174511&adults=2&childages=0&room=1"/> </rate> </ratelist>
how avoid change in response ?
thanks in advance.
this correct behaviour. if not escaped, xml ill-formed (that is, parser must report error , fail parse it). if want generate xml, have escape &
characters.
the value of attributes contains &
. way represent it, lexically, in xml write &
. if parse xml , ask, say, value of attribute string, see has been decoded parser.
you can think way escape special characters in java or c++ string, using backslashes. string value not contain backslash, have use them represent lexically special characters.
Comments
Post a Comment