Solving development problems  |  About this blog

Archive for the ‘multimedia’ tag

ArrayOfXElement not recognized by Silverlight 2.0

Last weekend I was working on a Silverlight 2.0 game that loads and saves scores to an external database. Database is accessed via web service which has two methods: GetScore and SaveScore. GetScore returns DataSet and SaveScore returns nothing.

Web service proxy is generated as usual – right click on the project node in VS2008, “Add Service Reference…”, enter url, proxy class is generated and the methods are accessible in the code. But when I tried to build the project an error occured: “The type or namespace name ‘ArrayOfXElement’ does not exist in the namespace” which means that ArrayOfXElement is unrecognized class for the compiler. Searching on the Google reveals that this happens when service method returns DataSet, so ArrayOfXElement is actually DataSet and DataSets are not supported in Silverlight 2.0.

A quick solution is to replace all ‘ArrayOfXElement’ words with ‘object’. Project would build that way but the data would have no meaning.

Another solution is to change the web service return type but this is not always editable by the client side developer.

Finally, workaround idea is to get DataSet schema and convert it to a class. How to do this?
1. Create a dummy WPF project.
2. Add web service reference to the WPF project. Return type is DataSet instead of ArrayOfXElement.
3. Write some code to call a web service methods that returns DataSet then extract xsd schema and save it to the disk. Build and run. Sample:
GameServiceClient client = new GameServiceClient();
DataSet ds = client.GetScore(1, “abc”, “”, DateTime.Now);
ds.WriteXmlSchema(@”C:\Temp\Score.xsd”);
4. So you have the xsd file. Now find ‘xsd.exe’ which is located in the same directory as ‘svcutil.exe’ (e.g. C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin). Run it with arguments:
xsd C:\Temp\Score.xsd /c
5. xsd tool generates Score.cs file which contains tables as classes and columns as properties. Include the generated file in the Silverlight project.
6. Replace all ‘ArrayOfXElement’ words with ‘NewDataSet’ or other class name, depends on the retrieved DataSet name and generated code.

Note that this is just an idea. So far the sample project builds fine but there is a problem with cross-domain security. Another story.

Url to the web service in this example:
http://www.silverlightclub.com/apps/GameService.asmx

References:
http://silverlight.net/forums/t/60518.aspx
http://blogs.msdn.com/suwatch/archive/2009/01/21/wcf-silverlight-exception-and-serialization.aspx

Written by Avivo

March 21st, 2009 at 8:26 pm