0

我有一个类似以下布局的文档(实际上它要大得多):

<div id="header">
    <div id="whatever">
        <div id="content">
            <p>Yes</p>
        </div>
    </div>
</div>

我正在从其他地方导入该 html,但我无法控制它。我需要在倒数第二个 div 之前添加一点 html:即

<div id="header">
    <div id="whatever">
        <div id="content">
            <p>Yes</p>
        </div>

        [In here!]

    </div>
</div>

现在,我已经使用 substr 和 lastindexof 完成了其中的一部分。但我之前不太清楚如何到达 div。

这是我所拥有的:

Public Partial Class RightMenuControl
    Inherits System.Web.UI.UserControl

    Private dtItemsFeed As dsItems.ItemsFeedDataTable
    Private taItemsFeed As New dsItemsTableAdapters.ItemsFeedTableAdapter
    Private allText As String


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        loadData()
    End Sub

    Private Sub loadData()

        dtItemsFeed = New dsItems.ItemsFeedDataTable
        dtItemsFeed = taItemsFeed.GetCode()

        For Each rFeed As dsItems.ItemsFeedRow In dtItemsFeed
            allText = rFeed.SideCode
        Next

        Dim FirstDiv As String = allText.LastIndexOf("</div>")
        Dim LastDiv As String = allText.Substring(FirstDiv, 6)

        Dim str As String = allText

        Dim strRes As String = str.Insert(FirstDiv, "<div>extrahtml</div>")

        ltrSide.Text = strRes


    End Sub
End Class

将我的 extrahtml div 打印在:

<div id="header">
    <div id="whatever">
        <div id="content">
            <p>Yes</p>
        </div>
    </div>
        <div>extrahtml</div> 
</div>

汤姆

4

1 回答 1

0

好的,知道它总是倒数第二个 Div,这应该可以解决问题。

Dim LastDiv As Int = allText.LastIndexOf( "</div>" )
Dim SecondToLastDiv As Int = allText.LastIndexOf( "</div>", LastDiv ) - 1

Dim str As String = allText
Dim strRes As String = str.Insert(SecondToLastDiv, "<div>extrahtml</div>")
于 2012-08-15T12:42:09.910 回答