Got more questions? Find advice on: ASP | SQL | Regular Expressions | Windows
in Search
Welcome to XmlAdvice Sign in | Join | Help

Kirk Allen Evans' XML Blog

.NET From a Markup Perspective

Using Typed DataSets with Web Services

Someone asked me about using a typed DataSet on both ends of the wire using a web service. I honestly don't work with typed DataSets, but this question was to accomodate the client's existing architecture. We both tried it, and couldn't get it to work. This shouldn't be that hard,so I asked quite a few people... nada. Most admitted they just frankly don't use typed DataSets.So, I settledin behind the keyboard to fight through it. The answer turned out to be simple, but non obvious.

Suppose you have a typed DataSet and you want to use that with a Web Service. If you plan on exposing that web service to any clients not running .NET (such as VB6 using the Soap Toolkit), then you should not return a typed DataSet from a web service method. For more background on the interoperability problems, read Aaron Skonnard's "Web Services and DataSets" article to see the effect on the WSDL when returning a DataSet. Aaron's article shows that one workaround is to use an XmlDataDocument and return that to the client as an XmlNode type. I quote that article often, but hadn't actually tried to implement the solution.

I created a C# Class Library project file and left the name as ClassLibrary1. I added an XML Schema to the project, and dragged some tables from Northwind to the design surface. Then I tweaked the schema for a little while (I am a control freak about the XML Schema layout, no big deal). The reason for the library is so that I can create a typed DataSet that is used by both the WinForms client and the web service.

In the same .sln, I added a C# Web Service project and named it WebService1 (clever, huh?). I set a project reference to ClassLibrary1 above so that I could use the typed DataSet. Here is the HelloWorld code, uncommented and modified:

[WebMethod]
public XmlNode HelloWorld()
{
System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection("Data Source=L00261;User ID=sa;Password=asdfasdf;Initial Catalog=Northwind;");
cn.Open();
System.Data.SqlClient.SqlDataAdapter adap = new System.Data.SqlClient.SqlDataAdapter("SELECT * FROM CUSTOMERS",cn);
ClassLibrary1.Document s = new ClassLibrary1.Document();
adap.Fill(s,"Customers");
adap.Fill(s,"Orders");
adap.Fill(s,"OrderDetails");

adap.Dispose();
cn.Close();
cn.Dispose();
System.Xml.XmlDataDocument doc = new XmlDataDocument(s);
return doc;
}

Using this web service method, the contents of the SOAP body will be the same as if you used:

s.WriteXml(Response.OutputStream, XmlWriteMode.IgnoreSchema);

Once you have the web service running and you can invoke it from the ?wsdl test page (for example, my new service was http://localhost/webservice1/service1.asmx?wsdl), you can create the client.

Create a WinForms client and set a project reference to ClassLibrary1. Then create a Web Reference to your service, using the WSDL endpoint specified above. Drop a DataGrid and a Button onto Form1. Double-click the button and add the following code:

localhost.Service1 s = new localhost.Service1();
System.Xml.XmlNode n = s.HelloWorld();
ClassLibrary1.Document x = new ClassLibrary1.Document();
x.ReadXml( new System.Xml.XmlNodeReader(n),XmlReadMode.IgnoreSchema );
this.dataGrid1.DataSource = x;

The trick to using the typed DataSet on both ends of the wire is that you have to specify XmlReadMode.IgnoreSchema, or you will not see any data show up in your DataGrid control.

Published Thursday, February 26, 2004 6:58 PM by kaevans
Filed under: ,

Comments

 

kaevans said:

I guess we are on the same wavelength in some way with different solutions. I happened to write about this exact topic for my installment of my DataStream column in asp.netNOW this week based on a recent question from a guy on the ado.net newsgroups I helped. Should be out any day.

The solution I proposed was to simply merge the data from the web service created typed data set into the class library referenced one. Works fine because the underlying schema will be the same as long as they originated from the same typed data set definition. Net result is the same, but yours is a better solution because of not copying over all the data.

Thanks!
February 27, 2004 8:33 PM
 

kaevans said:

March 9, 2004 4:02 PM
 

kaevans said:

Hi, I'm using VS.NET 2003 and cannot get this to work. I'm doing exactly as your example states. The error message is something like:
Error in XML Document (1, 672)

I think the line causing the error is:
System.Xml.XmlNode n = s.HelloWorld();

Any ideas?
March 24, 2004 1:51 AM
 

kaevans said:

It sounds like the serialization of the typed dataset yielded a character that is not supported. You can use a tool like MSSoapT from the SOAP Toolkit (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsoap/html/soap_faq.asp) or tcpTrace (http://www.pocketsoap.com) to trace the soap payload. You can then put the resulting XML into a .XML file and open it with Internet Explorer, or you can put the XML into the VS.NET XML designer and click the button to indent the document. Either of these actions will check well-formedness.

There might be a problem with the encoding for the response (should be UTF-8) which causes the problem. The DataSet is serialized as XML, and the serializer should use the correct encoding for the response.
March 24, 2004 12:06 PM
 

kaevans said:

I guess I'm about a year late on this post... but I ran across it doing some searches for strongly type datasets.

Anyway, I agree with you that the return type of a webservice shouldn't be a strongly typed dataset, this really limits who can use your webservice. However, I've seen several instances where returning "string" data that is actually xml that references a schema works well as a loosely coupled solution for both ends. Granted the WSDL won't show this, but if there is any documentation on the service, you document that your return data is xml according to the xsd. This way people who want to creatly strongly type datasets (or other classes like it) can reference the public schema, and the old school developers can just create the xml using an xmldoc, xpath, a stringbuilder or another format.
January 12, 2005 2:59 PM
 

TrackBack said:

February 26, 2004 7:08 PM
 

Kirk Allen Evans' Blog said:

Avoid returning XmlNode from a web service method at all costs. In this post, I show why you should avoid returning XmlNode in web services, and demonstrate how to use the IXmlSerializable interface in .NET 2.0 web services for high performance XML serialization.
April 6, 2006 3:45 PM
 

Using Typed DataSets with Web Services said:

November 27, 2007 7:30 PM
 

Dodge Spirit Cars Grand Caravan, Dodge Spirit Club said:

May 20, 2010 5:18 PM
 

Vw Eurovan Instructions, Eurovan Transmission Volkswagen said:

May 20, 2010 6:23 PM
 

Apollo Repair Answer, Apollo Horizon said:

May 20, 2010 6:40 PM
 

Volvo 262 Totale Lengte Cm, Newegg Sager 9262 said:

May 20, 2010 11:10 PM
 

Logitech Mx510 Optical Mouse, Download Mx5 said:

May 21, 2010 12:10 AM
 

Amanti Radiator Fit Tail Light Assembly, Kia Amanti Promotion Toyota Avalon said:

May 21, 2010 5:47 AM
 

R500 Directory, Samsung Sch R500 Manual said:

May 21, 2010 6:46 AM
 

Cj3 Dealers, Discount Jeep Cj3b said:

May 21, 2010 4:09 PM
 

Gs350 Part 2009 Lexus Gs 450h Rear Wheel Drive, Brother 9450cdn Mfc 9440cn said:

May 21, 2010 7:44 PM
 

2001 Mercedes C320 Misfire, Fog 2003 Mercedes Benz C320 Tail Light said:

May 22, 2010 6:21 AM
 

2007 Ford Econoline E350 Mpg Stabilizer Bar, E350 Accessories Air Intakes said:

May 22, 2010 12:42 PM
 

99 Mercury Cougar Headlight Infiniti Jeep, Cougar Replacement Call Grand Marquis said:

May 22, 2010 2:35 PM
 

Mercedes E 350 Sale E320 Bluetec Sedans, E300 Aftermarket E320 Mercedes Benz said:

May 22, 2010 5:47 PM
 

Capri Bulb Grand Marquis 2002 Mercury Mountaineer, Mercury Taillight New Mountaineer said:

May 22, 2010 8:51 PM
 

Cj5a Putt, Jeep Cj5a Headlight said:

May 22, 2010 9:59 PM
 

Buy 533i 1984 Bmw, Bmw 533i Brake Rotors Auto Parts - 383.cmanager.org said:

May 23, 2010 7:24 AM
 

Transmission Fluid Dipstick Es350, Gs350 Pt Replacement Retractable - 68.zapstreaming.com said:

May 23, 2010 9:07 AM
 

760li Seats Interior, 760li Promo - 20.tgrconversions.com said:

May 24, 2010 9:00 AM
 

W300 Collection Headlight Fits, Pathfinder Part Today Nissan 300zx - 107.cmanager.org said:

May 24, 2010 4:50 PM
 

Aftermarket 1998 Bmw 318ti, 318ti Fog Light Bulb Bmw 318is - 461.akemet.com said:

May 24, 2010 6:24 PM
 

Starfire Heater Cates, Starfire Animated Episode Guide - 93.eumreborn.com said:

May 24, 2010 8:23 PM
 

380sel Replacement Fuel Pump 1983 Mercedes Benz, 380sel Auction Oe - 351.zapstreaming.com said:

May 24, 2010 8:31 PM
 

Fiesta Grease Pan Stainless Steel, Used Fiesta For Sale - 279.ja3ra.com said:

May 25, 2010 5:09 AM
 

Diamante Poem Outline, Diamante Price Used Mitsubishi - 331.tgrconversions.com said:

May 25, 2010 4:54 PM
 

1996 - 1988 @ Impulse Used Isuzu Stylus, Epson Stylus D78 Cartridges - 40.renters.ws said:

May 31, 2010 3:30 AM
 

1980 - 1990 @ Forester Wrx Sti, Xt Club 2005 Subaru Forester - 180.codebluehacks.org said:

May 31, 2010 4:05 PM
Anonymous comments are disabled

This Blog

Syndication

News

Looking for a place to talk about XML? Tired of the "main feed police" cracking about your interests in football and politics? Sign up for a free web log on XMLAdvice.com.