从HttpWebRequest XML VB.net填充DropDownList

时间:2016-03-22 17:28:42

标签: asp.net vb.net

我是VB.net的新手,我无法弄清楚如何从HttpWebRequest将XML数据加载到DropDownList中。我想要加载到DropDownList的数据是Merchant + Price。下面是我的代码:

Dim s As HttpWebRequest
s = HttpWebRequest.Create("myURL")
s.Method = "GET"
Dim postresponse As HttpWebResponse
postresponse = DirectCast(s.GetResponse, HttpWebResponse)

Dim postreqreader As New Stream(postresponse.GetResponseStream())

Dim returnData As String = postreqreader.ReadToEnd

postresponse.Close()

我从returnData返回的XML响应是:

<xml version="1.0" encoding="UTF-8" ?>
<Response>
    <ID>123</ID>
    <Items>
        <Item>
            <Isbn>123456789</Isbn>
            <Offers>
                  <Offer>
                      <Isbn>123456789</Isbn>
                      <Merchant>Vendor1</Merchant>
                      <Price>3.00</Price>
                      <Shipping>8.00<Shipping>
                  </Offer>
                  <Offer>
                      <Isbn>123456789</Isbn>
                      <Merchant>Verndor2</Merchant>
                      <Price>3.00</Price>
                      <Shipping>8.00<Shipping>
                  </Offer>
             </Offers>
        </Item>
    </Items>
</Response>

2 个答案:

答案 0 :(得分:0)

首先使用此代码将xml转换为数据集

    DataSet ds;
    StringReader reader = new StringReader(string);
    ds.ReadXml(reader);

然后将其绑定到DropDownList

答案 1 :(得分:0)

首先确保关闭发货标记。

尝试这样的事情:

Dim ds As New DataSet()
ds.ReadXml(new XmlTextReader(new StringReader(returnData )))

ds.Tables("Offer").Columns.Add("MerchantPrice", GetType(String), "Merchant + ' - ' + Price")

Dim dv As DataView = ds.Tables("Offer").DefaultView
dv.Sort = "Merchant"

ddlMerchant.DataTextField = "MerchantPrice"
ddlMerchant.DataValueField = "Isbn"
ddlMerchant.DataSource = dv
ddlMerchant.DataBind()