我在本地计算机上运行了一个 Web 服务http://localhost:8000/Service/当我导航到它时,它会以列表格式显示一些硬编码信息,如下所示:
<ArrayOfStudent>
<Student>
<StudentID>bla</StudentID>
<FirstName>bla</FirstName>
<LastName>bla</LastName>
</Student>
<Student>
<StudentID>bla1</StudentID>
<FirstName>bla1</FirstName>
<LastName>bla1</LastName>
</Student>
<Student>
<StudentID>bla2</StudentID>
<FirstName>bla2</FirstName>
<LastName>bla2</LastName>
</Student>
</ArrayOfStudent>
如何获取这样的 xml 列表并将其从我的 uri http://localhost:8000/Service/添加到列表框中,因为它已完成我无法添加对 Windows 窗体应用程序的服务引用?
例如,这是一种如何将图像从 uri 添加到图像框的方法:
public Image GetImage(int width, int height)
{
string uri = string.Format("http://localhost:8000/Service/picture/{0}/{1}", width, height);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
return new Bitmap(stream);
}
}
}
然后我所要做的就是pictureBox1.Image = GetImage(pictureBox1.Height, pictureBox1.Width);
在我想要的任何地方打电话。我只是不知道如何从我的服务中添加文本数据?
我正在尝试这样的事情:http: //www.dotnetcodecentral.com/Post/215/wcf-rest-sumption/convert-or-deserialize-wcf-rest-response-to-objects-list
private void button2_Click(object sender, EventArgs e)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream strm = resp.GetResponseStream();
XElement xdoc = XElement.Load(strm); // XElement.Load has some invalid arguements?
q = From student In xdoc.<Student>// from here
Select New With {
.StudentNo = student.<StudentID>.Value,
.Firstname = student.<FirstName>.Value,
.Surname = student.<LastName>.Value,
}; // to here is abit of a mess
listBox1.DataSource = q.ToList();
}
}
编辑- 格式问题?