【问题标题】:VB.NET ~ Need help parsing XML loaded in richtextbox (loaded from URL)VB.NET ~ 需要帮助解析在richtextbox 中加载的XML(从URL 加载)
【发布时间】:2011-12-15 02:39:24
【问题描述】:

我的屏幕上有两个富文本框和两个按钮。第一个按钮从 URL 中获取 HTML,然后将 HTML 转换为位于富文本框 1 中的 XML。

第二个按钮是从富文本框 1 中抓取 XML,然后解析它以通过 ID 抓取所有输入元素。

我的问题是我的解析器没有做任何事情。我的猜测是我并没有完全从第一个富文本框中获取 XML。

从富文本框中获取 XML 并将其加载到内存中然后解析 XML 以获取所有 ID 标记的最佳方法是什么?

这是我的代码 -- 感谢您的帮助。

Imports mshtml
Imports System.Text
Imports System.Net
Imports System.Xml
Imports System.IO
Imports System.Xml.XPath

Public Class Scraper

    Private Sub Scraper_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '  Note: This example uses two Chilkat products: Chilkat HTTP
        '  and Chilkat HTML-to-XML.  The "Chilkat Bundle" can be licensed
        '  at a price that is less than purchasing each product individually.
        '  The "Chilkat Bundle" provides licenses to all existing Chilkat components.  Also, new-version upgrades are always free.

        Dim http As New Chilkat.Http()

        '  Any string argument automatically begins the 30-day trial.
        Dim success As Boolean
        success = http.UnlockComponent("30-day trial")
        If (success <> True) Then
            TextBox1.Text = TextBox1.Text & http.LastErrorText & vbCrLf
            Exit Sub
        End If

        Dim html As String
        html = http.QuickGetStr("http://www.quiltingboard.com/register.php")
        If (html = vbNullString) Then
            TextBox1.Text = TextBox1.Text & http.LastErrorText & vbCrLf
            Exit Sub
        End If

        Dim htmlToXml As New Chilkat.HtmlToXml()

        '  Any string argument automatically begins the 30-day trial.
        success = htmlToXml.UnlockComponent("30-day trial")
        If (success <> True) Then
            TextBox1.Text = TextBox1.Text & htmlToXml.LastErrorText & vbCrLf
            Exit Sub
        End If

        '  Indicate the charset of the output XML we'll want.
        htmlToXml.XmlCharset = "utf-8"

        '  Set the HTML:
        htmlToXml.Html = html

        '  Convert to XML:
        Dim xml As String
        xml = htmlToXml.ToXml()

        '  Save the XML to a file.
        '  Make sure your charset here matches the charset
        '  used for the XmlCharset property.
        htmlToXml.WriteStringToFile(xml, "out.xml", "utf-8")

        RichTextBox1.Text = xml
    End Sub

    Private Sub LoopThroughXmlDoc(ByVal nodeList As XmlNodeList)
        For Each elem As XmlElement In nodeList
            If elem.HasChildNodes Then
                LoopThroughXmlDoc(elem.ChildNodes)
            Else
                '' Extract the information
                If elem.HasAttribute("id") Then
                    'elem.Attributes("AssetID").Value.ToString()
                ElseIf elem.HasAttribute("name") Then
                    'elem.Attributes("AttributeID").Value.ToString()
                End If
            End If
        Next
    End Sub

    Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim doc As XmlDocument = New XmlDocument
        doc.Load("xmlFile.xml")
        Dim nodeList As XmlNodeList = doc.GetElementsByTagName("input")
        LoopThroughXmlDoc(nodeList)
    End Sub
End Class

【问题讨论】:

    标签: xml vb.net parsing


    【解决方案1】:

    第二个按钮不会从 RichTextBox 中获取 XML,它会尝试从 xmlFile.xml 加载它。

    此文件与保存在 button1 中的文件不同,即 out.xml。

    如果用户可以更改richtextbox中的XML,那么解决方法是更改​​button2中的代码以从RTB中检索文本。

    否则,解决方法是将button2中正在读取的文件名改为out.xml。

    【讨论】:

      猜你喜欢
      • 2023-01-25
      • 1970-01-01
      • 2011-02-26
      • 1970-01-01
      • 1970-01-01
      • 2015-09-10
      • 2019-06-27
      • 2020-08-08
      • 1970-01-01
      相关资源
      最近更新 更多