我有 .xml 文件,我想按名称标题快速搜索。我创建了用于键入名称的按钮搜索和文本框。但总是弹出 "item not found"
i have .xml file and i want search quickly by name title. i create button search and textbox for type the name. but always popup "item not found"
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
search()
End Sub
Private Sub search()
Dim input As String = txtSearch.Text.Trim
If input.Length = 0 Then
Exit Sub
End If
Dim xmlFile As XmlReader
xmlFile = XmlReader.Create("C:\Bison\POS\Memo\memo_index.xml", New XmlReaderSettings())
Dim ds As New DataSet
Dim dv As DataView
ds.ReadXml(xmlFile)
dv = New DataView(ds.Tables(0))
dv.Sort = "title"
Dim dex As Integer = dv.Find("txtHeader.Text")
If dex = -1 Then
MsgBox("Item Not Found")
Else
MsgBox(dv(dex)("title").ToString() & " '" & input & "%' " & dv(dex)("file").ToString())
'& dv(index)("Product_Price".txtSearch).ToString())
End If
样本XML:
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<Table>
<title>Memo - Best Customer Service Award (Hari B C ).</title>
<file>041115170756.pdf</file>
<creation>04/11/2015 5:07:56 PM</creation>
</Table>
<Table>..</Table>
</NewDataSet>
您必须在没有 "" 的 DataView 方法 Find 中使用 txtSearch.Text
,如下所示:
Dim dex As Integer = dv.Find(txtSearch.Text)
DataView的Find方法,根据指定的排序键值在DataView中查找一行,必须在搜索TextBox中输入正确的名称标题。
它很适合我,您可以在下面查看我的示例:
XML 文件:
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<Table>
<title>Memo - Best Customer Service Award (Hari B C ).</title>
<file>041115170756.pdf</file>
<creation>04/11/2015 5:07:56 PM</creation>
</Table>
<Table>
<title>Test</title>
<file>041115170756.pdf</file>
<creation>04/11/2015 5:07:56 PM</creation>
</Table>
</NewDataSet>
代码:
Private Sub ButtonSearch_Click(sender As Object, e As EventArgs) Handles ButtonSearch.Click
Dim xmlFile As XmlReader
xmlFile = XmlReader.Create("C:\Bison\POS\Memo\memo_index.xml", New XmlReaderSettings())
Dim ds As New DataSet
Dim dv As DataView
ds.ReadXml(xmlFile)
dv = New DataView(ds.Tables(0))
dv.Sort = "title"
Dim dex As Integer = dv.Find(txtSearch.Text)
DataGridView1.DataSource = dv
If dex = -1 Then
MsgBox("Item Not Found")
Else
MsgBox("Item Found")
End If
End Sub
测试结果:
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
search()
End Sub
Private Sub search()
Dim input As String = txtSearch.Text.Trim
If input.Length = 0 Then
Exit Sub
End If
Dim xmlFile As XmlReader
xmlFile = XmlReader.Create("C:\Bison\POS\Memo\memo_index.xml", New XmlReaderSettings())
Dim ds As New DataSet
Dim dv As DataView
ds.ReadXml(xmlFile)
dv = New DataView(ds.Tables(0))
dv.Sort = "title"
Dim dex As Integer = dv.Find("txtHeader.Text")
If dex = -1 Then
MsgBox("Item Not Found")
Else
MsgBox(dv(dex)("title").ToString() & " '" & input & "%' " & dv(dex)("file").ToString())
'& dv(index)("Product_Price".txtSearch).ToString())
End If
样本XML:
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<Table>
<title>Memo - Best Customer Service Award (Hari B C ).</title>
<file>041115170756.pdf</file>
<creation>04/11/2015 5:07:56 PM</creation>
</Table>
<Table>..</Table>
</NewDataSet>
您必须在没有 "" 的 DataView 方法 Find 中使用 txtSearch.Text
,如下所示:
Dim dex As Integer = dv.Find(txtSearch.Text)
DataView的Find方法,根据指定的排序键值在DataView中查找一行,必须在搜索TextBox中输入正确的名称标题。
它很适合我,您可以在下面查看我的示例:
XML 文件:
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<Table>
<title>Memo - Best Customer Service Award (Hari B C ).</title>
<file>041115170756.pdf</file>
<creation>04/11/2015 5:07:56 PM</creation>
</Table>
<Table>
<title>Test</title>
<file>041115170756.pdf</file>
<creation>04/11/2015 5:07:56 PM</creation>
</Table>
</NewDataSet>
代码:
Private Sub ButtonSearch_Click(sender As Object, e As EventArgs) Handles ButtonSearch.Click
Dim xmlFile As XmlReader
xmlFile = XmlReader.Create("C:\Bison\POS\Memo\memo_index.xml", New XmlReaderSettings())
Dim ds As New DataSet
Dim dv As DataView
ds.ReadXml(xmlFile)
dv = New DataView(ds.Tables(0))
dv.Sort = "title"
Dim dex As Integer = dv.Find(txtSearch.Text)
DataGridView1.DataSource = dv
If dex = -1 Then
MsgBox("Item Not Found")
Else
MsgBox("Item Found")
End If
End Sub
测试结果: