.net SOAP 消息中布尔值的正确编码是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4069596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:52:20  来源:igfitidea点击:

What is the correct encoding of a Boolean value in a SOAP message?

.netsoap

提问by Chris Roberts

We have produced a web service in VB.NET against a WSDL file provided to us by our client.

我们已经根据客户提供给我们的 WSDL 文件在 VB.NET 中生成了一个 Web 服务。

In testing, our client has raised an issue complaining that the XML produced by our web service uses 'true' and 'false' for boolean values, not '1' and '0'. Furthermore, they are suggesting that our implementation is 'broken' because of this.

在测试中,我们的客户提出了一个问题,抱怨我们的 Web 服务生成的 XML 使用“true”和“false”作为布尔值,而不是“1”和“0”。此外,他们暗示我们的实施因此而“中断”。

I was under the impression that 'true/false' and '1/0' in this context were interchangeable?

我的印象是“真/假”和“1/0”在这种情况下可以互换?

Either way, all of the XML serialisation is being handled by the .NET framework, not our code - so I assume I have little chance of changing it anyway?!

无论哪种方式,所有 XML 序列化都由 .NET 框架处理,而不是我们的代码 - 所以我认为我无论如何都没有机会改变它?!

Can anyone point me to some documentation which either backs up my story or proves me wrong? If I am wrong, does anyone know how I might be able to alter this behaviour?

谁能给我指出一些支持我的故事或证明我错的文件?如果我错了,有谁知道我如何能够改变这种行为?

Thanks in advance...

提前致谢...

回答by Cameron Skinner

From the SOAP spec: 'the boolean lexical forms "1" and "true" are interchangeable.'

来自SOAP 规范:“布尔词法形式“1”和“真”是可以互换的。

Not much more to say, really.

不多说了,真的。

回答by Wim ten Brink

Not WSDL- related but XML-related: a customer once claimed they could not read our XML files and insisted that we would generate XML files according to the standard. Unfortunately, their standard was different from the official standard. For example, they could not parse comments, had troubles with the XML header and they expected the XML to be nicely indented with linebreaks after every element. Why? Because they wrote their own XML parser, that was unable to follow the true standard.
Since our software is also used by other clients, we first wanted to send them "The Finger" and just let them disappear quietly. But one person from marketing found a nicer solution and one of our developers was sent to this client to give additional advise about the correct usage of XML. We educated the client, who suddenly realized that they didn't have to write their own parser. We helped them to improve their own code base and made them quite happy since we solved the problem by educating them. And all this for a small, additional fee so marketing was happy too. :-)

There is an alternative, though! Replace booleans with your own enumerations. For example yes/no or something similar. The advantage is that you can then expand those enumerations with additional values like yes/no/maybe or even yes/no/filenotfound...
Still, booleans are often preferred and to keep the SOAP messages human-readable, you better use true/false since people tend to make mistakes when you use 1/0. Why? Simple, 1 is true? Or False? In some programming languages, -1 means true. And in older languages, 1 meant false. (Because in those languages, a boolean was a kind of error-bit, which -if set- equals 1 thus indicates an error.)
Readability should therefor mean that you should prefer to use true/false or else end up discussing why 1 means true...

与 WSDL 无关,但与 XML 相关:一位客户曾声称他们无法读取我们的 XML 文件,并坚持我们将根据标准生成 XML 文件。不幸的是,他们的标准与官方标准不同。例如,他们无法解析注释,在处理 XML 标头时遇到问题,并且他们希望 XML 在每个元素后都能很好地缩进并带有换行符。为什么?因为他们自己编写了XML解析器,所以无法遵循真正的标准。
由于我们的软件也被其他客户使用,我们首先想发送给他们“手指”,让他们安静地消失。但是营销部门的一个人找到了一个更好的解决方案,我们的一位开发人员被派往这个客户,就 XML 的正确用法提供额外的建议。我们教育了客户,他们突然意识到他们不必编写自己的解析器。我们帮助他们改进了他们自己的代码库,并让他们非常高兴,因为我们通过教育他们解决了问题。所有这一切都需要一笔小额的额外费用,因此营销也很高兴。:-) 不过

还有一个选择!用您自己的枚举替换布尔值。例如是/否或类似的东西。优点是您可以使用附加值(如 yes/no/maybe 或什至)扩展这些枚举yes/no/filenotfound...
不过,布尔值通常是首选,并且为了保持 SOAP 消息人类可读,您最好使用 true/false,因为当您使用 1/0 时人们往往会犯错误。为什么?很简单,1是真的吗?还是假的?在某些编程语言中,-1 表示为真。在较旧的语言中,1 表示错误。(因为在这些语言中,布尔值是一种错误位,它 -if set-equals 1 因此表示错误。)
因此,可读性应该意味着您应该更喜欢使用 true/false 或最终讨论为什么 1 意味着真的...

回答by MusiGenesis

Your choices are:

您的选择是:

  1. Tell the client that you're right (which you are) and that trueand falseare perfectly valid boolean values in SOAP. And then deal with an angry client.

  2. Change your boolean properties to ints.

  1. 告诉客户你是对的(你是),并且truefalse在SOAP完全有效的布尔值。然后与愤怒的客户打交道。

  2. 将布尔属性更改为整数。

I'd go with #2, myself, just for the harmony of it all.

我会选择#2,我自己,只是为了这一切的和谐。