How to consume Webservices
Hi,
Im quite new to webservices. These are my questions.
1. Im just wondering on how to consume webservices. For example, I have a webmethod that returns a Dataset object how would I consume it in the client? I tried casting the result to DataSet object and it just wont convert...
2. I've read in a forum that its not advisable to return a complex class/structure (eg Dataset Class) instead, stick to the simple structured xml. My question is, how would you do that? How can you return a simple structured xml in your webmethod? And how would you consume it in the client side?
BTW, my front end is ASP.net and im trying to consume the webserv there...
cheers,
Paul June A. Domag
For question 1, are you using VisualStudio.Net? If so, then when you add a web reference, the resulting generated class should automatically type the return value as DataSet. I use DataSets
heavily in my web services, and have had no problem using them. If you're not using VS.Net, then you can still take the XML that is returned from the web service, create an instance of DataSet, then use the ReadXml() method to "inflate" the XML into a DataSet.
For question 2, I think this depends on how varied your consumers will be. If most of them will be using tools that consume WSDL and generate strongly-typed code, then by all means, use complex classes. If you have some folks out there that will be using visual notepad as their development tool and you don't feel like punishing them for it, you might want to keep things more simple.
Keep in mind that no matter what you return, everything becomes structured XML, it's just up to you what format that XML takes.
hth,
Todd
Hi,
I'm currently using VS.Net 2005 Beta2. Could you please show me a code snippet on how to handle "Webmethods" that returns a Dataset object? I'm really puzzled here. In my Webservice, I specified to return a Dataset. But in my ASP.Net webform, It doesn't return a Dataset, I tried casting it to no avail...
cheers,
Paul June A. Domag
Hi,
Ok, got the problem. I wasn't actually returning a Dataset but a datatable instead. Is datatable not supported as a return type in asp.net? can't seem to return a datatable value...
Another inquiry, If I have a datatable in my webservice how could I convert it to xml and return it? I know that upon returning a datatable a xml is automatically returned. But I want a simple type xml to be returned, not a complex one...
Last, Could anyone point me to a online tutorial, or a document that has these kind of samples? I really got lost on MSDN Docs...
cheers,
Paul June A. Domag
Hi Paul,
To answer your first question, yes, its not supported to pass a datatable or a datarow as a return value of a web service since it is not serializable.
Read KB Article 306134 for more information.
If you need to get the Xml, then I guess you would need to use a DataSet and pass it into a XmlDataDocument class and retrieve the Xml.
Also check the GetXml method on the Dataset class - but be warned that the GetXml will build Xml for only those columns that have values. If you have a column that does not have a value then it will not appear as an element in the Xml.
Regards,
Vikram
Paul,
You can convert a DataTable to XML very easily by using the WriteXML method. Here's an example bit of code:
| | System.Data.DataSet ds; ... Do something to fill the DataSet ... System.IO.StringWriter sw = new System.IO.StringWriter(); ds.Tables[0].WriteXml(sw); sw.Close(); string tablexml = sw.ToString(); |
The WriteXml method has lots of overloads as well to control whether schema is written, child tables are written, etc. Keep in mind that data tables store XML (by default) in the following fashion:
<NewDataSet>
<SampleTable>
<Column1>Sample Value</Column1>
<Column2>123</Column2>
<Column3>True</Column3>
</SampleTable>
<SampleTable>
<Column1>Another Sample</Column1>
<Column2>456</Column2>
<Column3>False</Column3>
</SampleTable>
</NewDataSet>
The above example represents a table called SampleTable with columns: Column1 varchar(50), Column2 int, Column3 bit. The root node uses the "Name" property of the DataSet. If this is the format of XML that you want to pass back, then you're all set. If you want it in another format, you may have some work to do. Perhaps an XSL transform?
Also keep in mind that (again, by default) if a value is null, the <Column> tag is omitted altogether.
I say "by default" because you can control how XML is persisted within XML Serialization. In the DataSet designer in VS.Net 2003 (which was really just the XML Schema Designer), you could easily specify whether a column was persisted as an attribute or as an element, and you could set the minoccurs to 1 to ensure that the <Column> tag was emitted. I haven't been able to find how to do that in the new DataSet designer; if anyone else knows how, you could save me some time finding it...
Another idea you may want to investigate: If you are using a stongly-typed dataset (I highly recommend this), then you can extend the generated code, adding a method that will produce the XML you want to pass back (double-click on the table in the dataset designer). That encapsulates the funcitonality nicely with the data. If I just went over-the-top on you, feel free to ignore this paragraph. I just thought I would toss it in there.
One last caveat. For those who are not using .net 2.0, the WriteXml method does not exist on the DataTable class in .net 1.x. You will have to use the GetXml (or WriteXml) method on the DataSet object and load that into an XmlDocument object, then select the table nodes using XmlDocument.SelectNodes().
hth,
Todd