XML文档中的Findnext()方法
本文关键字:方法 Findnext 文档 XML | 更新日期: 2023-09-27 18:04:45
还有一个关于XML文档的问题。因此,我想通过消息正文(文本)来查找消息我写了find()方法:
public void find(string searchText)
{
XmlNodeList smss = xmlDoc.SelectNodes("//sms");
searchText = txtFind.Text;
foreach (XmlNode sms in smss)
{
if (sms.Attributes["body"].InnerText.Contains(searchText))
{
txtName.Text = sms.Attributes["body"].InnerText +
sms.Attributes["time"].Value;
break;
}
}
}
此方法查找包含搜索字符串的第一条消息并将其写入txtName文本框。现在,我想找到下一个消息包含相同的字符串,并将其添加到txtname文本框,换句话说,我想写findNext()方法。我不知道该怎么做。
您可以使用for循环并存储计数器的状态,然后从XMLNodeList中跳转计数器元素并从那里重新开始搜索,例如:
//Assumes i is declared somewhere else
for (i ; i < smss.Count; i++)
{
if (smss[i].Attributes["body"].InnerText.Contains(searchText))
{
txtName.Text = smss[i].Attributes["body"].InnerText +
smss[i].Attributes["time"].Value;
break;
}
}
我不确定你是否在寻找这样的东西,但希望下面的例子可以帮助你
Private Sub CommandButton1_Click()
If OptionButton1 Then
Dim MyValue, MyFindNext
[A1].Select
MyValue = TextBox1.Value
On Error GoTo err_Trap
Cells.Find(What:=MyValue, After:=ActiveCell, LookIn:=xlValues, LookAt:= _
xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Activate
MyFindNext = vbYes
Do Until MyFindNext <> vbYes
MyFindNext = MsgBox("Would you like to find the next instance of " & MyValue & "?", _
vbYesNo, "Find Next")
If MyFindNext = vbNo Then
Unload UserForm1
Exit Sub
End If
Cells.FindNext(After:=ActiveCell).Activate
Loop
On Error GoTo 0
Exit Sub
End If
If OptionButton2 Then
[A1].Select
MyValue = TextBox1
On Error GoTo err_Trap
Cells.Find(What:=MyValue, After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Activate
MyFindNext = vbYes
Do Until MyFindNext <> vbYes
MyFindNext = MsgBox("Would you like to find the next instance of " & MyValue & "?", _
vbYesNo, "Find Next")
If MyFindNext = vbNo Then
Unload UserForm1
Exit Sub
End If
Cells.FindNext(After:=ActiveCell).Activate
Loop
On Error GoTo 0
Exit Sub
err_Trap:
If Err.Number = 91 Then
MsgBox "Could not find " & MyValue & " anywhere on this sheet.", , "Search failed"
Else
MsgBox Err.Number & ": " & Err.Description
End If
End If
End Sub
更多信息请参考此链接
http://www.knowexcel.com/view/931222-findnext-method-help-.html