1

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.

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/>

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.

Any suggestions?

Thanks a lot!!

PaulG
  • 6,920
  • 12
  • 54
  • 98
  • This post: http://stackoverflow.com/questions/9639312/can-you-add-additional-properties-to-net-serialized-null-values speaks about namespaces but you have to use the same attribute. – Adriano Repetti May 28 '12 at 14:57
  • Do you have minOccurs="0" attribute on C element in schema? – Ritesh May 28 '12 at 15:30

1 Answers1

3

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.

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

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

<C></C>

because how would You differ between empty string and 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.

Community
  • 1
  • 1
Grzegorz W
  • 3,487
  • 1
  • 21
  • 21