HTMLの各要素にアクセスするたびに、RootElementを呼びなおさなければならないのは、少々面倒です。ここでは、IEWrapperクラスに加えて、IEWrapperFixedクラスを作成し、IEオブジェクトをコピーして保持しておけるようにします。
ソースコードは、以下のようになります。まず、IEWrapperクラスには、System.ICloneableインタフェースを実装しています。次に、IEWrapperFixedクラスです。使い方は例えば以下のようにします。
Public Class IEWrapper
Implements System.ICloneable
'コピーコンストラクタ
Sub New(ByVal ieSrc As IEWrapper)
_ie = ieSrc._ie
_currentFrame = ieSrc._currentFrame
_currentDoc = ieSrc._currentDoc
_currentElement = ieSrc._currentElement
End Sub
Private Function Clone() As Object Implements System.ICloneable.Clone
Return Me.MemberwiseClone()
End Function
Function ShallowCopy() As IEWrapper
Return DirectCast(Me.Clone(), IEWrapper)
End Function
End Class
Public Class IEWrapperFixed
Private ReadOnly _ieFixed As IEWrapper
Private Sub New()
End Sub
Sub New(ByVal ieSrc As IEWrapper)
_ieFixed = ieSrc.ShallowCopy()
End Sub
Function GetIE() As IEWrapper
Return _ieFixed.ShallowCopy()
End Function
End Class
Dim objTable As New IEWrapperFixed(ie.RootDocument().Element("table", 1))
For i As Integer = 0 To ie.Element("tr").length - 1
ie = objTable.GetIE()
objTr = New IEWrapperFixed(ie.Element("tr", i))
orderNo = ie.Element("td", 0).InnerText
log.Info(orderNo)
ie = objTr.GetIE()
companyId = ie.Element("td", 2).InnerText
log.Info(companyId)
Next
スポンサーリンク