fc2ブログ

IEオブジェクトをコピーして利用する

HTMLの各要素にアクセスするたびに、RootElementを呼びなおさなければならないのは、少々面倒です。ここでは、IEWrapperクラスに加えて、IEWrapperFixedクラスを作成し、IEオブジェクトをコピーして保持しておけるようにします。
スポンサーリンク
ソースコードは、以下のようになります。まず、IEWrapperクラスには、System.ICloneableインタフェースを実装しています。
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
次に、IEWrapperFixedクラスです。
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
スポンサーリンク
<<フォーム操作2(ラジオボタン、チェックボックス、セレクトボックス) | ホーム | 証券会社のWeb画面をクラス化する>>
コメント(0)
コメントの投稿
トラックバック(0)