响应 xml 中未显示空元素(SOAP Web 服务) [英] null elements not showing in response xml (SOAP web service)

查看:35
本文介绍了响应 xml 中未显示空元素(SOAP Web 服务)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我们的客户创建了一个 asp.net Web 服务,该服务返回多个包含复杂类型的 List 对象.客户端一直在正常接收数据.

I've created an asp.net web service for a client of ours which returns multiple List objects containing complex types. The client has been receiving data just fine.

尽管他注意到并非所有标签都一直存在.例如,如果我在我的列表中返回 3 个对象,对象 A B 和 C.当我返回它时,C 为空.它会显示:

He has noticed though that not all the tags are there all the time. If, for example, I return 3 objects in my list, objects A B and C. and C is null when I return it. It will show:

<A>data</A>
<B>data</B>

当我希望它显示以下内容时:

When I would like it to show the following:

<A>data</A>
<B>data</B>
<C/>

或者至少:

<A>data</A>
<B>data</B>
<C></C>

基本上,无论哪些数据可用,哪些数据为空,我都希望 XML 的结构保持不变.

Basically, I want the structure of the XML to remain the same regardless of what data is available and what data is null.

有什么建议吗?

非常感谢!!

推荐答案

肥皂规范指出:

空值或默认值可以通过省略访问器元素来表示.NULL 值也可以由包含属性 xsi:null 的访问器元素指示,值为1"或其他可能的应用程序相关属性和值.

A NULL value or a default value MAY be represented by omission of the accessor element. A NULL value MAY also be indicated by an accessor element containing the attribute xsi:null with value '1' or possibly other application-dependent attributes and values.

不幸的是,对于这种情况,MS 选择了第一个选项.

Unfortunately for You MS has chosen first option for this situation.

另一方面,您不能像这样创建 NULL 值:

On the other hand you could not make NULL value like this:

<C></C>

因为空字符串和空字符串有什么区别?

because how would You differ between empty string and null?

这个选项

<C/>

看起来很有希望.我猜您的服务使用 XMLSerializer 来序列化您的数据.检查这篇文章.也许您可以为您的对象实现 IXMLSerializable 并为您的数据提供自定义 xml.

looks promising. I'm guessing your service uses XMLSerializer to serialize your data. Check this post. Maybe you can implement IXMLSerializable for your object and provide custom xml for your data.

这篇关于响应 xml 中未显示空元素(SOAP Web 服务)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆