2012-08-31 45 views
0

我想生成一個列表(字符串)作爲下拉列表的數據源使用。 我已經做了這麼多次,但這個版本並沒有像往常一樣分離出來。xmlnodelist下拉

下面是一個XML樣本

<fueltypes> 
<fuel> 
    <type>Marine Diesel NY Harbor</type> 
    <dbheader>NYMarineDiesel</dbheader> 
</fuel> 
<fuel> 
    <type>ULSD NY Harbor</type> 
    <dbheader>NYULSD</dbheader> 
</fuel> 
</fueltypes> 

下面是

Public Shared Function GetFuelTypes(ddlControl As Control) As List(Of String) 
    Dim doc As New XmlDocument() 

    'Load XML from the file into XmlDocument object 
    doc.Load("H:\OtherDataFiles\dataXML.xml") 'this needs to be changed to the server path 
    Dim root As XmlNode = doc.DocumentElement 

    'Select all nodes with the tag paramter indicated by the nodestring variable 
    Dim nodeList As XmlNodeList = root.SelectNodes("fueltypes") 
    Return (From node As XmlNode In nodeList Select node.InnerText).ToList() 
End Function 

他們獲取綁定到下拉菜單中的功能如下

'load the fuel types into the dropdownlist 
    ddlFuelTypes.DataSource = GetFuelTypes() 
    ddlFuelTypes.DataBind() 
    ddlFuelTypes.SelectedIndex = 1 

下拉顯示一個所有項目line

回答

0

您需要完全限定nodeList,以便您可以訪問包含所需文本的子元素,例如

Dim nodeList As XmlNodeList = root.SelectNodes("/fueltypes/fuel/type") 

或者,您需要循環訪問nodeList並閱讀所需的元素,例如,

Dim nodeList As XmlNodeList = root.SelectNodes("/fueltypes/fuel") 

For Each fuelNode In nodeList 
    Dim fuelType = fuelNode.ChildNodes.Item(0).InnerText 
+0

注:/在不需要/ fueltypes /燃料/類型的字符串的前面,並且會導致錯誤,在代碼行昏暗節點列表作爲XmlNodeList中= root.SelectNodes(「/ fueltypes /燃料/鍵入「) – dinotom

+0

它一旦移除就可以正常工作 – dinotom