当在ASP中进行xml序列化时,如何防止'&'在'&'中转换。净Web API ?

[英]How do I prevent '&' to convert in '&' while xml serialization in ASP.NET Web API?


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

我正在使用ASP创建一个预订服务。净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,

响应必须是XML格式的,所以我将其序列化为如下,

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

my Global.asax File

我的世界。asax文件

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.

提前谢谢。

2 个解决方案

#1


2  

This is correct behaviour. If it was not escaped, the XML would be ill-formed (that is, a parser would must report an error and fail to parse it). If you want to generate XML, you have to escape the & characters.

这是正确的行为。如果没有转义,则XML将是不良格式(也就是说,解析器必须报告错误并不能解析它)。如果要生成XML,必须转义&字符。

The value of the attributes contains an &. The way to represent it, lexically, in XML is to write it as &amp;. If you parse the XML and ask, say, the value of the attribute as a string, you will see it has been decoded properly by the parser.

属性的值包含一个&。在XML中,用词法表示它的方法是将它写成&如果解析XML并询问属性作为字符串的值,则会看到解析器正确地对其进行了解码。

You can think about it like the way you escape special characters in a Java or a C++ string, using backslashes. The string value does not contain any backslash, but you have to use them to represent lexically some special characters.

您可以把它想象成使用反斜杠来转义Java或c++字符串中的特殊字符的方式。字符串值不包含任何反斜杠,但您必须使用它们来按词法表示某些特殊字符。

#2


0  

You have two mutually contradictory requirements:

你有两个互相矛盾的要求:

(a) the output should be XML

(a)输出应该是XML。

(b) the output should contain an unescaped "&"

(b)输出应包含一个未转义的“&”

XML can't contain an unescaped ampersand.

XML不能包含未转义的&号。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2015/06/25/5373993008f6ed74a23adddd94891197.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号