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.
我为我们的客户创建了一个asp.net Web服务,它返回包含复杂类型的多个List对象。客户端一直在接收数据。
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:
他注意到并不是所有的标签都在那里。例如,如果我在列表中返回3个对象,则返回时,对象A B和C.以及C为null。它将显示:
<A>data</A>
<B>data</B>
When I would like it to show the following:
当我希望它显示以下内容时:
<A>data</A>
<B>data</B>
<C/>
Or at least:
或者至少:
<A>data</A>
<B>data</B>
<C></C>
Basically, I want the structure of the XML to remain the same regardless of what data is available and what data is null.
基本上,我希望XML的结构保持不变,无论可用的数据是什么,哪些数据为null。
Any suggestions?
有什么建议么?
Thanks a lot!!
非常感谢!!
1 个解决方案
#1
1
Soap spec. states:
肥皂规格状态:
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.
可以通过省略存取器元素来表示NULL值或默认值。 NULL值也可以由包含属性xsi:null且值为“1”的访问器元素指示,或者可能由其他依赖于应用程序的属性和值指示。
Unfortunately for You MS has chosen first option for this situation.
不幸的是,MS为这种情况选择了第一个选项。
On the other hand you could not make NULL value like this:
另一方面,您无法像这样创建NULL值:
<C></C>
because how would You differ between empty string and null?
因为空字符串和null之间的区别如何?
This option
这个选项
<C/>
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.
看起来很有希我猜你的服务使用XMLSerializer来序列化你的数据。查看这篇文章。也许你可以为你的对象实现IXMLSerializable并为你的数据提供自定义xml。
#1
1
Soap spec. states:
肥皂规格状态:
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.
可以通过省略存取器元素来表示NULL值或默认值。 NULL值也可以由包含属性xsi:null且值为“1”的访问器元素指示,或者可能由其他依赖于应用程序的属性和值指示。
Unfortunately for You MS has chosen first option for this situation.
不幸的是,MS为这种情况选择了第一个选项。
On the other hand you could not make NULL value like this:
另一方面,您无法像这样创建NULL值:
<C></C>
because how would You differ between empty string and null?
因为空字符串和null之间的区别如何?
This option
这个选项
<C/>
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.
看起来很有希我猜你的服务使用XMLSerializer来序列化你的数据。查看这篇文章。也许你可以为你的对象实现IXMLSerializable并为你的数据提供自定义xml。