Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have one Wcf rest service which returns list<student>. But the problem is that when I consume the service, data is returned in byte[].

How can I convert it to list<student> so that I can bind it to Gridview.

Following code works fine.
C#
byte[] data = proxy.DownloadData("http://localhost:6236/Service1.svc/GetData");
       Stream stream = new MemoryStream(data);
       DataContractSerializer obj = new DataContractSerializer(typeof(string));
       string result = obj.ReadObject(stream).ToString();
       TextBox1.Text = result;


What changes do i need to do to solve my problem.

Thanks,
Dhaval
Posted
Updated 25-Nov-11 1:42am
v2
Comments
dhavaleng 25-Nov-11 7:23am    
[WebGet(UriTemplate = "/GetDataList", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
List<person> GetDataList();

Ideally your REST service should return Data in a XML/JSON format.
http://en.wikipedia.org/wiki/Representational_state_transfer

http://msdn.microsoft.com/en-us/library/dd203052.aspx

http://msdn.microsoft.com/en-us/netframework/cc950529

Once you get your Data from your REST service in XML format, you may use DataSet.ReadXml to populate your DataSet.

http://msdn.microsoft.com/en-us/library/system.data.dataset.readxml.aspx

And then bind your DataSet to your GridView.
C#
myGrid.DataSource = myDataset;
myGrid.DataBind();


Edit -
Your comments -
Quote:
[WebGet(UriTemplate = "/GetDataList", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] List<person> GetDataList();

In case if you return your details from your REST Service in JSON format, then refer below link for it's deserialization.

http://jgvimalan.wordpress.com/2011/02/22/converting-json-string-to-c-object-and-vice-versa/
 
Share this answer
 
v2
can you provide me any example if you have available.
 
Share this answer
 
Comments
RaisKazi 25-Nov-11 7:40am    
Please do not add your comments as a Solution. You may refer links provided in my Solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900