通过webservice发送对象,并在asp.net或silverlight中检索数据
本文关键字:net silverlight 数据 检索 asp 并在 webservice 对象 通过 | 更新日期: 2023-09-27 17:49:29
我使用silverlight开发一个windows小工具,我需要调用asp.net web服务,这是返回一个对象与一些数据。我想要接收那个对象并显示数据
我的代码在
下面Web服务代码
[WebMethod]
public userdata LogIn(string username,string user_password)
{
return udata;
}
return new userdata();
}
}
我的自定义类代码是
public class userdata
{
string name;
public string Name
{
get { return name; }
set { name = value; }
}
string userName;
public string UserName
{
get { return userName; }
set { userName = value; }
}
string department;
public string Department
{
get { return department; }
set { department = value; }
}
string designation;
public string Designation
{
get { return designation; }
set { designation = value; }
}
string email;
public string Email
{
get { return email; }
set { email = value; }
}
string mobile;
public string Mobile
{
get { return mobile; }
set { mobile = value; }
}
string ip;
public string Ip
{
get { return ip; }
set { ip = value; }
}
string id;
public string Id
{
get { return id; }
set { id = value; }
}
}
返回
<userdata>
<Name>Asik</Name>
<UserName>asikcse</UserName
><Department>technical</Department>
<Designation>Software Programmer</Designation>
<Id>1</Id>
</userdata>
在我的项目结束时,我添加了web参考作为参考,但没有任何解决方案。
LoginServiceSoapClient login = new LoginServiceSoapClient();
login.LogInCompleted+=new EventHandler<LogInCompletedEventArgs>(login_LogInCompleted);
login.LogInAsync(log._nameText, log._surnameText);
}
void login_LogInCompleted(object sender, LogInCompletedEventArgs e)
{
How can I retrieve this object returned by webmethod
}
所以你需要在silverlight应用程序中创建一个类来保存数据,我建议只是复制userdata类。然后,您需要向该端点发出请求以获取数据。这可以使用WebClient类来完成。然后使用DataContractSerializer将xml转换为对象。下面是一个例子:
WebClient webClient = new WebClient();
var data = webClient.DownloadString("<your endpoint>"); //You can do this async too
var serializer = new DataContractSerializer(typeof(UserData));
Byte[] bytes = Encoding.Unicode.GetBytes(data);
UserData userData;
using (MemoryStream stream = new MemoryStream(bytes))
{
userData = serializer.ReadObject(stream) as UserData;
}
有一些更聪明的方法。