xml 从 webservice 到列表框 [英] xml to listbox from webservice

查看:42
本文介绍了xml 从 webservice 到列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的本地机器上运行了一个 Web 服务,地址为 http://localhost:8000/Service/ 当我导航到它时,它会以列表格式显示一些硬编码信息,如下所示:

I have a web service running on my local machine at http://localhost:8000/Service/ when I navigate to this it displays some hardcoded information in list format like so:

<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 窗体应用程序的服务引用?

How can I take a xml list like this and add it to a listbox from my uri http://localhost:8000/Service/ as its done with rest I cant add in a service reference to the windows form app?

例如,这是一种如何将图像从 uri 添加到图像框的方法:

For instance here is a method on how to add an image to an imagebox from a 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); .我只是不知道如何从我的服务中添加文本数据?

Then all I have to do is call pictureBox1.Image = GetImage(pictureBox1.Height, pictureBox1.Width); anywhere I want. I just dont have a clue how to add text data from my service?

我正在尝试这样的事情:http://www.dotnetcodecentral.com/Post/215/wcf-rest-convert-or-deserialize-wcf-rest-response-to-objects-list

I was attempting something like this: http://www.dotnetcodecentral.com/Post/215/wcf-rest-consuming/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();
    }
}

编辑 - 格式问题?

推荐答案

XDocument xDoc = XDocument.Load(url);
var students = xDoc.Descendants("Student")
    .Select(n => new
    {
        StudentNo = n.Element("StudentID").Value,
        Firstname = n.Element("FirstName").Value,
        Surname = n.Element("LastName").Value
    })
    .ToList();

dataGridView1.DataSource = students;

这篇关于xml 从 webservice 到列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆