Conditional serialization of an element?

M

MikeL

Hello.

I've created a web service based on a message contract with a business
partner.

In the schema some elements are optional.

How do I prevent an optional element from being serialized into the response
XML (I don't want it to show up) if the value of the element is a particular
value?

Thanks in advance,

Mike
 
D

Dan Rogers

For reference types (strings, objects, etc), just make the value null -
null references aren't serialized.
For value types, you have to add a "friend" to your code... eg

public int myFoo;
public bool myFooSpecified;

To supress myFoo (which always has a value, which is by default false)
serialization, set myFooSpecified to false (the default). To make it get
serialized, set myFooSpecified to true.

No matter what your value type field (or property) is, a boolean friend
with the same first name and Specified at the end will do the trick.

--------------------
 
M

MikeL

Thanks for responding, Dan.

It didn't take me long until I figured that one out.

Thanks again,

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top