【问题标题】:Webservice sending null in integer fieldsWeb服务在整数字段中发送空值
【发布时间】:2011-05-31 20:57:36
【问题描述】:

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

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

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

<SomeField/>

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

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

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

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

【问题讨论】:

    标签: .net soap soap-client


    【解决方案1】:

    我不认为 .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);
     }
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 我认为如果我不需要以不同方式处理 null 和 0,这将起作用。
      【解决方案3】:

      这是他们代码中的一个错误。这与架构不匹配。 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>
      

      【讨论】:

      • 同意,但这不是我要更改的代码,所以我需要解决它。
      • 作为这些数据的使用者,您的职责之一是报告错误。这是一个错误。报告它然后解决它 - 不要只是解决它而不告诉他们他们的错误。您看到此错误的原因之一可能是没有人向他们报告。
      • 是的,所有此类情况都已发送给供应商。尽管他们认为这是 Microsoft SOAP 解析器的问题,而不是他们的输出。我现在可以工作了。如果他们解决了他们的问题,这将使我的一方更干净,并且如果他们从他们那里获得新的 WSDL,也不会对任何新开发人员造成破坏。
      • 向他们指出这个答案:它来自 XMLSpy,而不是来自 Microsoft。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-04
      相关资源
      最近更新 更多