alternative XML serialization - how?

S

Shone

My problem is that I want my class instance to be serialized
differently for different purposes. Suppose I want to exchange the
object between different components on the local network by
serializing it into XML, and therefore I defined the class with
included XML attributes:

<XmlRoot(ElementName:="results")> _
Public Class clsOneClass
<XmlElement("first")> _
Public value1 As Integer
<XmlElement("second")> _
Public value2 As Integer
<XmlIgnore)> _
Public Status As Boolean
End Class


So, this is working fine, and just like I want when I use
"xmlser.Serialize(output,obj)", where xmlser is defined as :
Dim xmlser As New XmlSerializer(...type of obj...)


But now I need to exchange this object via web services, actually this
type will be the return of a web method, and for this purpose (and
this only!) I want the public member "Status" to be passed along,
which means it needs to be serialized within the SOAP message.

But of course, web service "reads" the XML attributes of a class and
doesn't include "Status" into serialized SOAP msg.

How can I define and choose the alternative XML/SOAP serialization ?


More details:
On the client side, I let VS to automatically create the whole wrapper
for the web service it consumes (just added the web reference). I
don't implement any custom SOAP formatting, however I tried to
mess-around a little bit with the reference.vb. Namely, I tried to add
the Public member "Status" to the autogenerated skeleton class, but of
course that didn't solve the problem - because when web service
serializes it's response into SOAP, it doesn't include the "Status" of
the original "clsOneClass" object, so the same member on the client
side remains empty (actually default boolean = False).
I hope this was clear enough to the ws experts that know what I'm
talking about here.


Thank you very much!

Shone
 
R

Robert Bogue

Why not always serialize the status and run it through a XSLT to blow it
away for the service that doesn't want it?

Rob

--
Robert Bogue
Microsoft MVP Server-Networking
MCSE(NT4), MCSE (W2K), MCSA:Security (W2K), CNA, A+, Network+, IT Project+,
Server+, Security+, CDIA+, E-Biz+
Crowe Chizek and Co LLC
 
D

Dino Chiesa [Microsoft]

There is a way to tell the serializer whether a value-type field (like
Status in your case) is actually specified, or should be treated as "null".
For example, rather than "true or false" the values for a boolean field
might actually be "true, false, or not specified at all". In support of
this, the convention is to add boolean field to the type, , with the name
xxxxxSpecified

where xxxxx is replaced with the name of the value-type field you are
modifying.

In your case, the resulting type is like this:

<XmlRoot(ElementName:="results")> _
Public Class clsOneClass
<XmlElement("first")> _
Public value1 As Integer
<XmlElement("second")> _
Public value2 As Integer
<XmlElement> _
Public Status As Boolean
<XmlIgnore> _
Public StatusSpecified As Boolean
End Class


If StatusSpecified is set to true, then Status will be serialized. If
false, it will not be serialized.

http://msdn.microsoft.com/library/e...SerializationXmlIgnoreAttributeClassTopic.asp

or see here for more on that.
http://www.dotnet247.com/247reference/msgs/49/245748.aspx


Another option of course is to have 2 different types. The base type does
not include status, and the BasePlusStatus type includes Status. Use the
Base type when you want no Status, use the BasePlusStatus when you are doing
webservices.

-Dino
 
S

Shone

Thank you so much, this is exactly what I need. It's even easier than
I expected, I thought I would have to deal with XmlAttributeOverrides
and create my own xml attr. class...etc.
I didn't know about this "Specified" feature of Xml serializer. Neat!
 

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

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top