Silverlight 3 calls WCF/WebService to obtain "dataset"

Source: Internet
Author: User
Tags format definition

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.