如何使用ID子节点找到NAME和CITY?

时间:2014-03-01 06:12:12

标签: xml vb.net

<?xml version="1.0" encoding="utf-8"?>
<mainele>
  <strtele>
    <id>1</id>
    <Name>sahil</Name>
    <City>kadi</City>
  </strtele>
  <strtele>
    <id>2</id>
    <Name>imran</Name>
    <City>kadi</City>
  </strtele>
</mainele>

我使用以下代码从组合框中的xml文件中获取id。

Dim doc As New System.Xml.XmlDocument
        doc.Load("D:\sahil practice\sd.xml")
        Dim list = doc.GetElementsByTagName("id")
        For Each item As System.Xml.XmlElement In list
            Me.ComboBox1.Items.Add(item.InnerText)
        Next

当我在组合框中选择id = 1时,结果是“SAHIL”和“Kadi”......

1 个答案:

答案 0 :(得分:2)

试试这个

    Dim doc As New System.Xml.XmlDocument
    doc.Load("D:\sahil practice\sd.xml")
    Dim list = doc.GetElementsByTagName("strtele")
    Dim name, city As String
    For Each item As System.Xml.XmlElement In list
        If item.SelectSingleNode("id").InnerText = ComboBox1.SelectedItem Then
            name = item.SelectSingleNode("Name").InnerText
            city = item.SelectSingleNode("City").InnerText
            Exit For
        End If
    Next