I wrote an article earlierArticleCreate a universal WebService data access for the Silverlight project. If you want to use web service in Silverlight, you can convert the commonly used datatable into xelement and return it in web service, so as to implement the so-called universal data access service.
Today, if we use the WCF/Web Service when using silverlight3, we can defineDatasetAs the return value, the returned value can be obtained directly when the service is called in the Silverlight application.Arrayofxelement(If it is a datatable, an error is returned)
See the demo below (using WCF as an example)
WCF definition:
Code
Public Dataset executequery ( String SQL)
{
Try
{
String [] Temp = SQL. Split ( " ; " . Tochararray ());
Dataset DS = New Dataset ();
For ( Int I = 0 ; I < Temp. length; I ++ )
{
Sqldataadapter da = New Sqldataadapter (temp [I], con );
Da. Fill (DS, I. tostring ());
}
Return DS;
}
Catch (Exception ex)
{
Throw Ex;
}
}
Calls in Silverlight:
Note the type of E. Result, xelement. We only need to confirm the structure of this xelement and correct parse.
The analysis shows that the execution result of each SQL statement is parsed into two nodes.
Now let's look at the content of each node:
Node0:Data Structure in SQL statements
Node1:SQL Execution result, returned in XML format
We can see that the result is an XML, but it has its own format definition. We can parse this XML to get our results.
Code
VaR s = From item In Result. nodes [ 1 ]. Descendants ( " _ X0030 _ " )
Select New
{
Guid = Item. element ( " Guid " ). Value,
Name = Item. element ( " Name " ). Value,
Address = Item. element ( " Address " ). Value,
Dept = Item. element ( " Dept " ). Value,
};
foreach (VAR v in S)
{< br> MessageBox. show (v. guid + " -- " + v. name);
}
Note that "_ x0030" is defined in node0. Of course, if there are multiple SQL statements, multiple names will be defined in node0, for example, _ x0030 _, _ x0031 _. But this name is unknown if it is customized. It adds confusion when parsing.