469,898 Members | 1,547 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

Is it possible to pass a XML document as a Web Service parameter?

Hi all.

I would like to create two web services:
- The first one - ImportData - should take as an input parameter a XML
document and return an integer saying if passed XML document was valid
and was succesfully processed.
- The second one - ExportData - should take as an input parameter an
integer an return a XML document.

I would like to know if it is possible to do something like that. Do I
have to do some conversions e.g. parsing XML document into single
strings before sending it? Or maybe all I have to do is simply pass a
XML document as a parameter? Any sample code or links would be
appreciated.

Thanks in advance,
placek

Mar 29 '06 #1
6 8683
One more thing: these web services will be written using ASP.NET 2.0
and will be called from 'old' ASP.

Mar 29 '06 #2
Hi Placek,

First I'll answer your questions:

Yes, you can pass XmlDocuments between web services and return a
'result code' based on the success of the call. It might look something
like this:

[WebMethod]
public int XmlExampleMethod(XmlDocument xml)
{
return 1;
}

Now, I'll be so rude as to offer some advice where you didn't ask for
it. I hope you don't mind.

- On using Xml as a parameter in a webservice:

This is something I avoid wherever possible. Web services are supposed
to be self describing. That is, you look at the Web Service's contract
(interface) and you should be able to determine what it does, what you
need to supply and what it will return. If you use domain objects as
parameters and sensible method names then this is easy. Can you guess
what this does and what you need to pass in?

[WebMethod]
public void ShipOrder(Order order)

OK, that's easy. How about this?

[WebMethod]
public void ShipOrder(XmlDocument xml)

I can guess it ships an order - but I have no idea what I should pass
to it! The previous example required a strongly typed Order class such
as this:

public class Order
{
public int OrderNumber;
public int CustomerNumber;
}

It's easy for me to see what I need to do to ship an order now.

And, the best thing is that the ASP.NET framework will take care of
serializing the Order class into XML when it travels across the 'wire'
so you only have to deal with neat & tidy .NET objects. Beautiful!

- On returning an int to indicate success or failure:
This is the same as a result code and is generally considered bad
practice today, which is why we now have exceptions. If something has
gone wrong you should shout about it (throw an exception) rather then
subtly returning a 0 or 1 that the consumer has to check.

It's a bit like walking into a coffee shop and asking for a coffee. You
get your cup and you start walking out of the store when you realise
the cup is empty so you go back to the attendant. "This cup is
empty!?", "Sorry, we've run out of beans" he replies.

"Why didn't you just tell me?"
"I did, I gave you an empy cup."

Josh
http://www.thejoyofcode.com/

Mar 29 '06 #3
Thanks Josh, you helped me a lot.

About passing XML document as an input parameter:
My webservice will be used only by one or two other applications. Their
authors cooperate with me so they will know what to pass. However I
will consider this one more time. One more thing: both sides
(webservice app and client) should have the same definition of domain
object being passed as a parameter. What if one of the sides is not
able to create such object, because it is too complex for it? I am not
sure if such situation could happen, I am just wondering...

About throwing an exception:
So should I throw an exception inside my WebMethod when something goes
wrong? Which application will catch this exception? My webservice or
client application, which called my webservice?

Best regards,
placek

Mar 29 '06 #4
One more question: I have just tried to pass an instance of TestParam
class as an input parameter and I got an exception: "Cannot serialize
member Service+Param.table of type System.Collections.Hashtable,
because it implements IDictionary."

My TestParam class is the following:
public class Param
{
public int age;
public string name;
public Hashtable table;
}

I know that Hashtable is the source of the exception. Is there some way
to avoid throwing that exception?

Best regards,
placek

Mar 29 '06 #5
Hi Piotrek,
"My webservice will be used only by one or two other applications. Their authors cooperate with me so they will know what to pass."

I still think it's worth making your web service a real web service. If
nothing else it will
allow for easier testing with great tools like the web service studio
(http://www.gotdotnet.com/Community/U...-e916ebc4159c).

One more thing: both sides (webservice app and client) should have the
same definition of domain
object being passed as a parameter.

One of the tenets of Service Orientation is to sure Schema and not
Type. That is, you should share the loose shape of your objects and not
the exact type. This all happens automatically when you add a reference
as WSDL.exe (the application visual studio uses to create a web
reference) generates new classes that map to schema. They are not the
same class, they just have the same shape. This is what makes web
services so great at interoperability - even a java client could read
your WSDL and generate the appropriate domain classes.
"So should I throw an exception inside my WebMethod when something goes wrong? Which application will catch this exception? My webservice or
client application, which called my webservice?"
"I know that Hashtable is the source of the exception. Is there some way

to avoid throwing that exception?"
Yes, not use a hashtable? Sorry, I realise that isn't very helpful but
hashtables can't be serialized. I would think carefully about the
contents of your hashtable and consider creating a tighter domain
object to store you data.

What are you putting in there?

Josh
http://www.thejoyofcode.com/
Absolutely, the Web Service will convert this to a soap exception which
can be caught by your web service client.

Mar 30 '06 #6
Hi Josh.

In my app I have the following class:
public class Question
{
int questionId;
string originalContent;
string currentContent;
...
}

I wanted to insert instances of this class to hashtable and set key to
questionId. In that case I would be able to quickly find proper
question.

Now I know that passing a hasthable is impossible, so I will pass an
array of Question objects instead of hasthable. My webservice will then
process this array and convert it to hashtable.

Thanks a lot for your help Josh.

Piotrek

Mar 30 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by kaiwing18 | last post: by
1 post views Thread by Dion Heskett | last post: by
3 posts views Thread by Gopal Prabhakaran | last post: by
6 posts views Thread by =?Utf-8?B?emlubw==?= | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.