2016-03-22 124 views
2

我是VB.net的新手,我無法確定如何從HttpWebRequest將XML數據加載到DropDownList中。我想要加載到DropDownList中的數據是Merchant + Price。樓下是我的代碼:從HttpWebRequest XML填充DropDownList XML VB.net

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> 

回答

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() 
+0

感謝。這項工作。我如何將下拉菜單中的兩個或多個字段組合在一起? – Kluong

+0

當然。只更新了代碼以顯示兩個字段 – ElenaDBA

0

先使用此代碼將XML中的數據集

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

和然後將其綁定到DropDownList