Web 服务在整数字段中发送 null

发布于 2024-11-10 18:06:54 字数 369 浏览 4 评论 0原文

我正在尝试使用第三方网络服务。返回的字段之一定义为

<s:element name="SomeField" minOccurs="0" maxOccurs="1" type="s:int"/> 

在 SOAP 响应中,他们将字段发送为

<SomeField/>

这会导致 .Net 反序列化器抛出异常,因为空 xml 元素不是有效的整数。

处理这个问题的最佳方法是什么?

我尝试调整 wsdl 将字段标记为可为空,这确实将生成的字段标记为 int?但解串器仍然失败。

我可以将端点实现为服务引用或 Web 服务引用。

I am trying to use a 3rd party webservice. One of the fields that is returned is defined as

<s:element name="SomeField" minOccurs="0" maxOccurs="1" type="s:int"/> 

In the SOAP response they send the field as

<SomeField/>

This is causes the .Net deserializer to throw an exception as the empty xml element is not a valid integer.

What is the best way to handle this?

I have tried tweaking the wsdl to mark the field as nullable which does marked the generated fields as int? but the deserializer still fails.

I can implement the endpoint as either a service reference or as a web service reference.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜嗑 2024-11-17 18:06:54

我认为 .Net 反序列化器无法处理这个问题。

SomeField 的定义调整为字符串怎么样?这种方式可以检查空值,但您必须对实际值执行 Int32.Parse 。

<s:element name="SomeField" minOccurs="0" maxOccurs="1" type="s:string"/> 

访问器可以是:

 void int? GetSomeField()
 {
     if (someField == null) return null;
     return In32.Parse(someField);
 }

I don't think the .Net deserialiser can cope with this.

How about tweaking the definition of SomeField to a string. This way can check for a null, but you would have to do an Int32.Parse to the real value.

<s:element name="SomeField" minOccurs="0" maxOccurs="1" type="s:string"/> 

The accessor could be:

 void int? GetSomeField()
 {
     if (someField == null) return null;
     return In32.Parse(someField);
 }
赤濁 2024-11-17 18:06:54

您可以将默认值设置为 0。这样,如果未设置该值,它将发送 0。

<s:element name="SomeField" minOccurs="0" maxOccurs="1" default="0" type="s:int"/> 

You could set the default to 0. That way if the value is not set it will send 0.

<s:element name="SomeField" minOccurs="0" maxOccurs="1" default="0" type="s:int"/> 
甜味拾荒者 2024-11-17 18:06:54

这是他们代码中的一个错误。这与架构不匹配。 XMLSpy 说:

File Untitled6.xml is not valid.
Value '' is not allowed for element <SomeField>.
    Hint: A valid value would be '0'.
    Error location: root / SomeField
    Details
        cvc-datatype-valid.1.2.1: For type definition 'xs:int' the string '' does not match a literal in the lexical space of built-in type definition 'xs:int'.
        cvc-simple-type.1: For type definition 'xs:int' the string '' is not valid.
        cvc-type.3.1.3: The normalized value '' is not valid with respect to the type definition 'xs:int'.
        cvc-elt.5.2.1: The element <SomeField> is not valid with respect to the actual type definition 'xs:int'.

我通过以下模式得到了它:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="root">
        <xs:annotation>
            <xs:documentation>Comment describing your root element</xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence>
            <xs:element name="SomeField" minOccurs="0" maxOccurs="1" type="xs:int"/> 
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

和以下 XML:

<?xml version="1.0" encoding="UTF-8"?>
<root xsi:noNamespaceSchemaLocation="Untitled5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <SomeField/>
</root>

This is a bug in their code. That does not match the schema. XMLSpy says:

File Untitled6.xml is not valid.
Value '' is not allowed for element <SomeField>.
    Hint: A valid value would be '0'.
    Error location: root / SomeField
    Details
        cvc-datatype-valid.1.2.1: For type definition 'xs:int' the string '' does not match a literal in the lexical space of built-in type definition 'xs:int'.
        cvc-simple-type.1: For type definition 'xs:int' the string '' is not valid.
        cvc-type.3.1.3: The normalized value '' is not valid with respect to the type definition 'xs:int'.
        cvc-elt.5.2.1: The element <SomeField> is not valid with respect to the actual type definition 'xs:int'.

I got that with the following schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="root">
        <xs:annotation>
            <xs:documentation>Comment describing your root element</xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence>
            <xs:element name="SomeField" minOccurs="0" maxOccurs="1" type="xs:int"/> 
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

and the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<root xsi:noNamespaceSchemaLocation="Untitled5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <SomeField/>
</root>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文