xmlnodelist下拉列表

时间:2012-08-31 22:33:15

标签: xml html-select

我正在尝试生成一个(字符串)列表,用作下拉列表的数据源。 我已经做了很多次,但这个版本并没有像往常一样将这些项目分开。

这是一个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

下拉列表显示一行中的所有项目

1 个答案:

答案 0 :(得分: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