0

还有一个关于 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() 方法。我不知道该怎么做。

4

2 回答 2

1

您可以使用 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;
    }
}
于 2013-03-18T20:14:49.870 回答
0

我不太确定您是否正在寻找类似的东西,但希望以下示例可以帮助您

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

于 2013-03-18T19:51:32.503 回答