Create common WebService data access for the Silverlight Project

Source: Internet
Author: User
Before using Silverlight, We created our own WebService for universal data access. The developer passes an SQL statement to get a dataset. The datatable is used as the return value. In the Silverlight project, due to its limitations on datatable, We have to first get the datatable, and then create a Web Service (WCF) locally to convert the resulting datatable. Convert to an array or a generic set to meet the needs of Silverlight.
However, this process is complicated, and developers have made a lot of effort.
Here, we have made some changes to the original WebService: whenever the webmethod returned for dataset is added with a "coat", it is converted to an xelement and then returned to the caller with an XML file. Developers only need to use LINQ to XML to get the desired set through simple XML operations. This eliminates the need to create "own" services for each project.
The following is a simple demo to describe this operation:

Create an xelement for the able as the return value.
Code
Static   Void Main ( String [] ARGs)
{
// Get Data Source datatable
Datatable dt = Client. executequery (SQL). Tables [ 0 ];

// Create XML document
Xdocument Doc =   New Xdocument (
New Xelement ( " Tables " ,
New Xattribute ( " Xmlns " , "" ),
New Xelement ( " Table " ,
New Xattribute ( " Name " , " 0 " ),
New Xelement ( " Columns " ),
New Xelement ( " Rows " )))
);

Xelement Columns=Doc. element ("Tables"). Element ("Table"). Element ("Columns");

Foreach (Datacolumn col In DT. columns)
{
// Add an element Column
Columns. Add ( New Xelement ( " Column " ,
New Xattribute ( " Name " , Col. columnname ),
New Xattribute ( " Type " , Col. datatype. Name)
));
}

Xelement rows=Doc. element ("Tables"). Element ("Table"). Element ("Rows");

Foreach (Datarow row In DT. Rows)
{
// Add an element row
Xelement newrow =   New Xelement ( " Row " );
Rows. Add (newrow );

String Data =   Null ;
Foreach (Datacolumn col In DT. columns)
{
Switch (Col. datatype. Name)
{
Case   " Datetime " :
Data = (Row [col] = Dbnull. Value ?   "" : Convert. todatetime (row [col]). tostring ( " Yyyy/mm/dd hh: mm: SS " ));
Break ;
Default :
Data = (Row [col] = Dbnull. Value ?   "" : Row [col]. tostring ());
Break ;
}

//Add attribute for the newly added row
Newrow. Add (NewXattribute (Col. columnname, data)
);

}
}

Xelement=Doc. element ("Tables");

Console. writeline (element. tostring ());
}

Result:

Caller parsing xelement Code
Public   Static List < Basicinfo > Getbasicinfo (xelement element)
{
List < Basicinfo > Lists =   New List < Basicinfo > ();

// element is the retrieved response
var result = from item in element. descendants ( " row " )
select item;

Basicinfo info= Null;

Foreach(Xelement itemInResult)
{
Info= NewBasicinfo ();

Info. ID = ( Int ) Item. Attribute ( " ID " );
Info. Name = ( String ) Item. Attribute ( " Name " );
Info. parentid = ( Int ) Item. Attribute ( " Parentid " );
Info. Grade = ( Int ) Item. Attribute ( " Grade " );
Info. idx = ( Int ) Item. Attribute ( " Idx " );

Lists. Add (Info );
}

ReturnLists;
}

You can also simplify the writing of the above LINQ to XML statement as follows:

Code
VaR result = From item In Element. descendants ( " Row " )
Select New Basicinfo ()
{
ID = ( Int ) Item. Attribute ( " ID " ),
Name = ( String ) Item. Attribute ( " Name " ),
Parentid = ( Int ) Item. Attribute ( " Parentid " ),
Grade = ( Int ) Item. Attribute ( " Grade " ),
Idx = ( Int ) Item. Attribute ( " Idx " )
};

Lists=Result. tolist<Basicinfo>();

Note: The entity classes here are indispensable.

Code
  Public   Class Basicinfo
{
Public   Int Id { Set ; Get ;}

Public StringName {Set;Get;}

Public IntParentid {Set;Get;}

Public IntGrade {Set;Get;}

Public IntIdx {Set;Get;}
}

The Application of LINQ to XML makes the operation simple and practical.

 

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.