Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Web Services / January 2006

Tip: Looking for answers? Try searching our database.

Problem controlling SOAP (de)serialization

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Martijn Saly - 11 Jan 2006 16:14 GMT
I have a class that looks like this (simplified):

public class Field {
   public string Name;
   public string Value;
}

I'm creating a webmethod that has this class as an argument, and the
following SOAP fragment for that is expected:

<Field>
  <Name>string</Name>
  <Value>string</Value>
</Field>

I want to make this (de)serialized as:

<Field Name="string">string</Field>

This would, of course eat up less bandwidth, and it looks more logical
for a little name/value-class. I've tried adding [XmlAttribute("Name")]
to the Name property, and that works perfectly. So I get this:

<Field Name="string">
  <Value>string</Value>
</Field>

Which looks even more silly, I think. So how do I get the aforementioned
way of (de)serialization? Any thoughts/ideas?

Signature

Thanks,

Martijn Saly

Josh Twist - 11 Jan 2006 18:59 GMT
You need to decorate your class with the XmlAttributeAttribute:

using System.Xml.Serialization;

public class Field {
   [XmlAttribute()]
   public string Name;
   public string Value;
}

Josh
http://www.thejoyofcode.com/
Martijn Saly - 11 Jan 2006 22:47 GMT
> You need to decorate your class with the XmlAttributeAttribute:
>
[quoted text clipped - 8 lines]
> Josh
> http://www.thejoyofcode.com/

Like I said, that'll give me half of what I want:

<Field Name="string">
  <Value>string</Value>
</Field>

I can apply XmlAttribute to the Value property as well, but that's not what
I want:

<Field Name="string" Value="string"/>

Because I want this:

<Field Name="string">string</Value>

Signature

Thanks,

Martijn Saly

Josh Twist - 12 Jan 2006 09:10 GMT
Oops sorry - Martijn, mustn't have finnished reading your post
properly! My apologies:

public class Field {
   [XmlAttribute()]
   public string Name;

   [XmlText()]
   public string Value;
}
Martijn Saly - 12 Jan 2006 11:08 GMT
> Oops sorry - Martijn, mustn't have finnished reading your post
> properly! My apologies:
[quoted text clipped - 6 lines]
>     public string Value;
> }

Thanks :)
That seems to work perfectly, I didn't know it was that easy :)

So the response and request of my webservice calls are now properly
(de)serialized, but there's one little thingy left... the HTML pages
that .NET generates for a webservice indicate that the following XML for
my class will/should be in the response/request:

<Field Name="string" />

That might confuse webservice consumers, as this is not correct,
according to the actual output of a webmethod that returns an instance
of my class...

Signature

Thanks,

Martijn Saly

Josh Twist - 12 Jan 2006 13:02 GMT
I'm not sure there is much you can do about it but your service
consumers shouldn't pay too much attention tot the asmx help page
anyway. It's the WSDL that matters.

You could customise the the DefaultWsdlHelpGenerator page or remove it
altogether (http://www.15seconds.com/issue/040609.htm)?

Josh
http://www.thejoyofcode.com/
Martijn Saly - 12 Jan 2006 13:59 GMT
> I'm not sure there is much you can do about it but your service
> consumers shouldn't pay too much attention tot the asmx help page
[quoted text clipped - 5 lines]
> Josh
> http://www.thejoyofcode.com/

You're right, but if the client doesn't support WSDL (such as an old
version of Visual Objects, for example), the programmer hardcodes the
SOAP message into the source code (arg! but unfortunately it happens)
and he'll probably want to take a look at the help page, rather than
examining the whole WSDL ;)

Anyway, thanks for all the info, I'll take a look at customizing the
DefaultWsdlHelpGenerator.

Signature

Thanks,

Martijn Saly

Josh Twist - 12 Jan 2006 14:07 GMT
I see your point - but I'd recommend you publish your own documentation
in that case because the help file only goes 'one level deep' anyway.
So if you have any real depth to your classes most of the detail would
be missing.

Good luck!
Josh Twist - 12 Jan 2006 14:07 GMT
I see your point - but I'd recommend you publish your own documentation
in that case because the help file only goes 'one level deep' anyway.
So if you have any real depth to your classes most of the detail would
be missing.

Good luck!

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.