Answered by:
How to consume an array of structs returned by a web service?

Question
-
User508577675 posted
A sample web service app I have been practicing with has two web methods:
1) HelloWorld -- which I can read the data from no problem - just a string
2) ClientData[] -- which returns an array of struct objects -- this is where I have the question.Here is my instantiation of the webservice in my winform app for reading from HelloWorld
myWebSvc1.WebService1 s1 = new myWebSvc1.WebService1();
string y = s1.HelloWorld(); //--no problems so far here
Console.WriteLine(y);
//--but when I add the call to GetClientData() I have a problem
List<ClientData> ls = new List<ClientData>(s1.GetClientData(3)); //--problem here----------------------------------------------------------
public struct ClientData
{
public String Name;
public int ID;
}[WebMethod(CacheDuration = 30,
Description="Returns an array of Clients.")]
public ClientData[] GetClientData(int Number)
{
ClientData [] Clients = null;if (Number > 0 && Number <= 10)
{
Clients = new ClientData[Number];
for (int i = 0; i < Number; i++)
{
Clients[i].Name = "Client " + i.ToString();
Clients[i].ID = i;
}
}
return Clients;
}If I browse the .asmx file -- the sample xml returns the following if I enter 3 for GetClientData function param
<?xml version="1.0" encoding="utf-8" ?>
- <ArrayOfClientData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://codeproject.com/webservices/">
- <ClientData>
<Name>Client 0</Name>
<ID>0</ID>
</ClientData>
- <ClientData>
<Name>Client 1</Name>
<ID>1</ID>
</ClientData>
- <ClientData>
<Name>Client 2</Name>
<ID>2</ID>
</ClientData>
</ArrayOfClientData>From my winform app I get the following error messages when trying to add the GetClientData function call above --
Error 1 The best overloaded method match for 'System.Collections.Generic.List<DSS_2008.Form7.ClientData[]>.List(System.Collections.Generic.IEnumerable<DSS_2008.Form7.ClientData[]>)' has some invalid arguments
Error 2 Argument '1': cannot convert from 'DSS_2008.myWebSvc1.ClientData[]' to 'System.Collections.Generic.IEnumerable<DSS_2008.Form7.ClientData[]>'
Error1 The best overloaded method match for 'System.Collections.Generic.List<DSS_2008.Form7.ClientData[]>.List(int)' has some invalid arguments
Error 2 Argument '1': cannot convert from 'DSS_2008.myWebSvc1.ClientData[]' to 'int'
How can I fix this so that I can read the result into my winform app?Thanks
------------------------------------------------
//--sample web service
namespace MyService
{
using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.Services;public struct ClientData
{
public String Name;
public int ID;
}
/// <summary>
/// Summary description for WebService1.
/// </summary>
[WebService(Namespace="http://codeproject.com/webservices/",
Description="This is a demonstration WebService.")]
public class WebService1 : System.Web.Services.WebService
{
//--source of sample: http://www.codeproject.com/KB/webservices/myservice.aspx
private const int CacheHelloWorldTime = 10; // secondspublic WebService1()
{
//CODEGEN: This call is required by the ASP+ Web Services Designer
InitializeComponent();
}/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
}//WEB SERVICE EXAMPLE
//The HelloWorld() example service returns the string Hello World
//To build, uncomment the following lines then save and build the project
//To test, right-click the Web Service's .asmx file and select View in Browser
//
[WebMethod(CacheDuration = CacheHelloWorldTime,
Description="As simple as it gets - the ubiquitous Hello World.")]
public string HelloWorld()
{
return "Hello World bbb";
}[WebMethod(CacheDuration = 30,
Description="Returns an array of Clients.")]
public ClientData[] GetClientData(int Number)
{
ClientData [] Clients = null;if (Number > 0 && Number <= 10)
{
Clients = new ClientData[Number];
for (int i = 0; i < Number; i++)
{
Clients[i].Name = "Client " + i.ToString();
Clients[i].ID = i;
}
}
return Clients;
}
}
}Monday, January 3, 2011 4:28 PM
Answers
-
User508577675 posted
OK. I solved my problem. I was trying to create a ClientData object based on a local ClientData object that I declared within the winform app. What I needed to do was to create a ClientData object based on my webservice ClientData object. Here is how it looks:
List<myWebSvc1.ClientData> ls = new List<myWebSvc1.ClientData>(s1.GetClientData(3));
foreach (myWebSvc1.ClientData cd in ls)
{
Console.WriteLine(cd.ID.ToString() + " " + cd.Name.ToString());
}and here is the output inside the winform app:
0 Client 0
1 Client 1
2 Client 2--same as the xml from the .asmx file.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, January 3, 2011 6:52 PM
All replies
-
User508577675 posted
OK. I solved my problem. I was trying to create a ClientData object based on a local ClientData object that I declared within the winform app. What I needed to do was to create a ClientData object based on my webservice ClientData object. Here is how it looks:
List<myWebSvc1.ClientData> ls = new List<myWebSvc1.ClientData>(s1.GetClientData(3));
foreach (myWebSvc1.ClientData cd in ls)
{
Console.WriteLine(cd.ID.ToString() + " " + cd.Name.ToString());
}and here is the output inside the winform app:
0 Client 0
1 Client 1
2 Client 2--same as the xml from the .asmx file.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, January 3, 2011 6:52 PM -
User-1038610069 posted
Anyone know how to do this in a website? I have tried listbox and gridview but have had no success.
Monday, March 18, 2013 12:19 PM