C# と VB.NET の質問掲示板

わんくま同盟

ASP.NET、C++/CLI、Java 何でもどうぞ

C# と VB.NET の入門サイト


(過去ログ 18 を表示中)
■7244 / )  Re[7]: MSDNの目次が欲しい
□投稿者/ YAS (20回)-(2007/09/02(Sun) 18:07:43)
MSDNの目次のDOMを簡易的に解析して,TreeViewに表示するサンプルをつくってみました。
(VB2005のフォームにコピペすれば動きます。)
このあたりからふくらませてみるのはどうでしょうかね?

Public Class Form1

    Dim SplitContainer1 As New SplitContainer
    Dim WithEvents WebBrowser1 As New WebBrowser
    Dim TreeView1 As New TreeView

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.SplitContainer1.Dock = DockStyle.Fill
        Me.WebBrowser1.Dock = DockStyle.Fill
        Me.TreeView1.Dock = DockStyle.Fill
        Me.SplitContainer1.Panel1.Controls.Add(Me.WebBrowser1)
        Me.SplitContainer1.Panel2.Controls.Add(Me.TreeView1)
        Me.Controls.Add(Me.SplitContainer1)
        Me.WebBrowser1.Navigate("http://msdn2.microsoft.com/ja-jp/library/default(d=toc).aspx")
    End Sub

    Private Sub WebBrowser1_Navigating(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating
        TreeView1.Nodes.Clear()
        If Not e.Url.ToString.Contains("d=toc") Then
            Dim Url As String = e.Url.ToString
            Url = IIf(Url.EndsWith(").aspx"), Url.Replace(").aspx", ",d=toc).aspx"), Url.Replace(".aspx", "(d=toc).aspx"))
            DirectCast(sender, WebBrowser).Navigate(Url)
        End If
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        Dim Document As HtmlDocument = Me.WebBrowser1.Document
        Dim Tree As New TreeNode("<MSDN>")
        TreeView1.BeginUpdate()
        TreeView1.Nodes.Add(Tree)
        DumpNode(Document, Document.GetElementById("ctl00_DeepTreeControl1_MtpsDeepTree"), Tree)
        TreeView1.ExpandAll()
        TreeView1.EndUpdate()
    End Sub

    Private Sub DumpNode(ByVal Document As HtmlDocument, ByVal Element As HtmlElement, ByVal TreeNode As TreeNode)
        For Each cElement As HtmlElement In Element.Children
            Dim TagName As String = String.Empty
            Dim cTreeNode As TreeNode = TreeNode
            If cElement.Id IsNot Nothing Then
                If cElement.Id Like "ctl00_DeepTreeControl1_MtpsDeepTree*Nodes" OrElse _
                    cElement.Id Like "ctl00_DeepTreeControl1_MtpsDeepTreet*" Then
                    TagName &= " id = " & cElement.Id
                    If cElement.GetAttribute("title") <> String.Empty Then
                        TagName &= " title = " & cElement.GetAttribute("title")
                    End If
                    If cElement.TagName.ToLower = "a" AndAlso cElement.GetAttribute("href") <> String.Empty Then
                        TagName &= " href = " & cElement.GetAttribute("href")
                    End If
                    cTreeNode = TreeNode.Nodes.Add("<" & TagName & ">")
                End If
            End If
            If cElement.Children.Count > 0 Then DumpNode(Document, cElement, cTreeNode)
        Next
    End Sub

End Class

返信 編集キー/


管理者用

- Child Tree -