|
-
Jul 15th, 2008, 02:13 PM
#5
Re: [2008] Save webpage table to dataset
This is what I came up with... Form2 is just a form with 1 textbox, 1 button and a webbrowser.
Code:
Public Class Form2
Private songData As DataTable = Nothing
Public ReadOnly Property SongTable() As DataTable
Get
Return Me.songData
End Get
End Property
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.songData = New DataTable("SongData")
Dim primary As DataColumn
With songData.Columns
primary = .Add("Song Number", GetType(Integer))
.Add("Title", GetType(String))
.Add("Authors", GetType(String))
.Add("Copyright", GetType(String))
.Add("Catalogs", GetType(String))
.Add("Administrators", GetType(String))
.Add("Key", GetType(String))
.Add("Key Line", GetType(String))
.Add("Also Known As", GetType(String))
.Add("Themes", GetType(String))
.Add("Lyrics", GetType(String))
End With
Me.songData.PrimaryKey = New DataColumn() {primary}
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Navigate to the song detail page
WB1.Navigate(Me.TextBox1.Text)
End Sub
Private Sub WB1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WB1.DocumentCompleted
Dim sbLyrics As New System.Text.StringBuilder()
Dim tables As HtmlElementCollection = WB1.Document.GetElementsByTagName("table")
Dim rows As HtmlElementCollection = Nothing
Dim cells As HtmlElementCollection = Nothing
Dim songRow As DataRow = Me.songData.NewRow()
For Each table As HtmlElement In tables
rows = table.GetElementsByTagName("tr")
If rows.Count = 1 Then
cells = table.GetElementsByTagName("td")
For Each cell As HtmlElement In cells
Dim spanCollection As HtmlElementCollection = cell.GetElementsByTagName("span")
If spanCollection.Count > 0 Then
For Each itm As HtmlElement In spanCollection
Dim txt As String = itm.InnerText
If txt.IndexOf(Environment.NewLine) > 0 Then
sbLyrics.Append(txt)
End If
Next
songRow("Lyrics") = sbLyrics.ToString
End If
Next
ElseIf rows.Count = 12 Then
Dim id As Integer = -1
Dim colName As String = String.Empty
Dim value As String = String.Empty
songRow("Title") = rows(0).Children(0).InnerText
For i As Integer = 1 To 9
colName = rows(i).Children(0).InnerText
value = rows(i).Children(1).InnerText
If colName = "Song Number" Then
id = Integer.Parse(value)
songRow(colName) = id
Else
songRow(colName) = value
End If
Next
If Me.songData.Rows.Find(id) Is Nothing Then
Me.songData.Rows.Add(songRow)
End If
End If
Next
End Sub
End Class
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|