有什么方法可以控制 ASMX Web 服务中 float/double 的格式吗?
如果我有以下简单的类:
[Serializable]
public class Test
{
public double a = 0.0000001;
}
当我从 .NET ASMX Web 服务返回此类型的对象时,响应将是:
<Test>
<a>1E-07</a>
</Test>
这也许不是巧合,这是 0.0000001.ToString() 的默认行为code>,尽管至少使用 ToString
,我可以指定格式参数(在这种情况下,我不想要指数表示法。事实上,我什至不确定我如何将其挂接到 XmlSerializer
我可以完全控制它,更不用说仅从我可以放置在返回对象上的属性中
控制了:我仍然会。非常喜欢将属性保留在我实际将其应用于强类型的内容中。0.0000001 和 1E-7 都是 .NET 中的有效双重表示,我只想让它像另一个一样通过网络发送内容。
If I have the following simple class:
[Serializable]
public class Test
{
public double a = 0.0000001;
}
When I return an object of this type from a .NET ASMX Web Service, the response will be:
<Test>
<a>1E-07</a>
</Test>
which is, perhaps not coincidentally, the default behavior of 0.0000001.ToString()
, though at least with ToString
, I can specify the formatting parameters (in this case, I do not want exponential notation. In truth, I'm not even sure how I would hook this into an XmlSerializer
I had complete control over it, much less just from within the attributes I could place on the return object.
Edit: I'd still very much like to keep the property in what I'm actually applying this to strongly-typed. Both 0.0000001 and 1E-7 are valid double representations in .NET, I just want to have it send things across the wire as the other one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种简单的方法是将强类型属性标记为忽略,并仅出于 XML 序列化的目的提供另一个字符串属性。在它的 getter 中,您可以根据需要应用格式,并在 setter 中解析它。
A simple approach would be to mark the strongly-typed property as ignored and provide another string property for the sake of XML serialization only. Within its getter you can apply formatting as you need, and parse it back in the setter.
将使用与 XML Schema xs 兼容的表示形式通过网络发送双精度值:双数据类型。
任何明智的客户都应该能够解释这一点。如果您出于某种特定原因需要不同的表示形式,那么唯一现实的解决方案是使用以您想要的方式格式化的字符串。
A double will be sent across the wire using a representation that is compatible with the XML Schema xs:double datatype.
Any sensible client should be able to interpret this. If you have some specific reason for wanting a different representation, the only realistic solution is to use a string, formatted in the way you want.