如果知道其中一个子值,VB.net将获取XML属性id

时间:2015-03-20 09:38:33

标签: xml vb.net listbox

这是我的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<Patient_Data>
  <Patient ID="1">
    <Patient_Name>john</Patient_Name>
    <BPL_card_no.>1234</BPL_card_no.>
    <Room_no.>12</Room_no.>
    <Bed_no.>11</Bed_no.>
    <Admit_date>18-03-15</Admit_date>
    <Admit_time>02:24:17</Admit_time>
    <Discharge_date></Discharge_date>
  </Patient>
  <Patient ID="2">
    <Patient_Name>rambo</Patient_Name>
    <BPL_card_no.>123456</BPL_card_no.>
    <Room_no.>8</Room_no.>
    <Bed_no.>6</Bed_no.>
    <Admit_date>18-03-15</Admit_date>
    <Admit_time>02:24:33</Admit_time>
    <Discharge_date></Discharge_date>
  </Patient>
  <Patient ID="3">
    <Patient_Name>arnold</Patient_Name>
    <BPL_card_no.>12345678</BPL_card_no.>
    <Room_no.>88</Room_no.>
    <Bed_no.>99</Bed_no.>
    <Admit_date>18-03-15</Admit_date>
    <Admit_time>02:24:47</Admit_time>
    <Discharge_date>30/5/14</Discharge_date>
  </Patient>
  <Patient ID="4">
    <Patient_Name>john</Patient_Name>
    <BPL_card_no.>12345678</BPL_card_no.>
    <Room_no.>88</Room_no.>
    <Bed_no.>99</Bed_no.>
    <Admit_date>18-03-15</Admit_date>
    <Admit_time>02:25:33</Admit_time>
    <Discharge_date>2/3/14</Discharge_date>
  </Patient>
  <Patient ID="5">
    <Patient_Name>big show</Patient_Name>
    <BPL_card_no.>12345</BPL_card_no.>
    <Room_no.>66</Room_no.>
    <Bed_no.>7</Bed_no.>
    <Admit_date>18-03-15</Admit_date>
    <Admit_time>02:31:38</Admit_time>
    <Discharge_date></Discharge_date>
  </Patient>
</Patient_Data>

现在我有textbox,搜索buttonlistbox。 当用户键入名称并单击搜索按钮时,将运行以下查询:

Dim doc As XDocument = XDocument.Load(Environment.CurrentDirectory & "\database.xml")
For Each itm In doc.Descendants("Patient_Name")
            If itm = TextBox1.Text Then
            ListBox1.Items.Add(itm.Value)
            End If
Next

这会添加键入listbox的所有名称。在listbox selectedindexchanged事件之后,我有以下查询:

    Dim patientData = From el In doc.Element("Patient_Data").Elements("Patient")
Where el.Element("Patient_Name") = ListBox1.SelectedItem.ToString
        Select New With {.Name = el.Element("Patient_Name").Value, _
    .Bpl = el.Element("BPL_card_no.").Value, _
    .Room = el.Element("Room_no.").Value, _
    .Bed = el.Element("Bed_no.").Value, _
    .AdmitDt = el.Element("Admit_date").Value, _
    .DischargeDt = el.Element("Discharge_date").Value}
        For Each el In patientData
            TextBox2.Text = el.Name
            TextBox3.Text = el.Bpl
            TextBox4.Text = el.Room
            TextBox5.Text = el.Bed
            TextBox8.Text = el.AdmitDt
            TextBox6.Text = el.DischargeDt
        Next

这会将受尊重的名称相关数据显示到相应的文本框中。 问题是当用户搜索“john”时,列表框有两个项目,但选择任何列表框项目仅显示具有名字的人的数据。救命 谢谢。我想我可以使用id但我无法通过查询来获取所搜索名称的ID。

2 个答案:

答案 0 :(得分:0)

将datatable作为数据源分配到列表框并设置其DisplayMember和ValueMember属性。我认为以下代码应该有所帮助。

Dim doc As XDocument = XDocument.Load(Environment.CurrentDirectory & "\database.xml")
Dim dt As New DataTable
dt.Columns.Add("Name")
dt.Columns.Add("ID")
For Each itm In doc.Descendants("Patient_Name")
    If itm = "john" Then
        Dim dr As DataRow = dt.NewRow
        dr("Name") = itm.Value
        dr("ID") = itm.Parent.FirstAttribute
        dt.Rows.Add(dr)
    End If
Next
ListBox1.DataSource = dt
ListBox1.DisplayMember = "Name"
ListBox1.ValueMember = "ID"

在列表框selectedindexchanged事件中写入以下查询:

     Dim patientData = From el In doc.Element("Patient_Data").Elements("Patient") _
        Where (el.Element("Patient_Name") = ListBox1.Text.ToString) _
     Select New With {.Name = el.Element("Patient_Name").Value, _
   .Bpl = el.Element("BPL_card_no.").Value, _
   .Room = el.Element("Room_no.").Value, _
   .Bed = el.Element("Bed_no.").Value, _
   .AdmitDt = el.Element("Admit_date").Value, _
   .DischargeDt = el.Element("Discharge_date").Value, _
   .ParentID = el.Element("Admit_date").Parent.FirstAttribute}
        For Each el In patientData
            If el.ParentID.ToString = ListBox1.SelectedValue.ToString Then
               TextBox2.Text = el.Name
               TextBox3.Text = el.Bpl
               TextBox4.Text = el.Room
               TextBox5.Text = el.Bed
               TextBox8.Text = el.AdmitDt
               TextBox6.Text = el.DischargeDt
            End If
        Next

答案 1 :(得分:0)

您可以将XML转换为数据集,然后在数据集上查询,而不是查询XML

            Dim xmlFile As XmlReader
            xmlFile = XmlReader.Create(Environment.CurrentDirectory & "\database.xml", New XmlReaderSettings())
            Dim ds As New DataSet()
            ds.ReadXml(xmlFile)

            ListBox1.DataSource = ds.Tables(0)
            ListBox1.DisplayMember = "Patient_Name"
            ListBox1.ValueMember = "ID"

在列表框selectedindexchanged事件中写入以下查询:

            Dim drArr As DataRow() = CType(ListBox1.DataSource, DataTable).Select("ID = " & CInt(ListBox1.SelectedValue.ToString))
            If Not drArr Is Nothing AndAlso drArr.Length > 0 Then
                TextBox2.Text = drArr(0)("Patient_Name")
                TextBox3.Text = drArr(0)("BPL_card_no.")
                TextBox4.Text = drArr(0)("Room_no.")
                TextBox5.Text = drArr(0)("Bed_no.")
                TextBox8.Text = drArr(0)("Admit_date")
                TextBox6.Text = drArr(0)("Discharge_date")
            End If