2011-12-15 4 views
0

私は2つのリッチテキストボックスと2つのボタンを画面に持っています。最初のボタンはURLからHTMLを取得し、リッチテキストボックス1にあるXMLにHTMLを変換します。VB.NET〜richtextbox(URLからロード)に読み込まれたXMLを解析するのに役立つ

2番目のボタンは、リッチテキストボックス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 

答えて

0

2番目のボタンは、RichTextBoxからXMLを取得せず、xmlFile.xmlからXMLをロードしようとします。

このファイルは、button1に保存されているファイル(out.xml)とは異なります。

ユーザーがリッチテキストボックス内のXMLを変更できる場合は、ボタン2のコードを変更してRTBからテキストを取得します。

解決方法は、button2で読み取られているファイルの名前をout.xmlに変更することです。

+0

ありがとうございましたCompetent_tech – user1096419

関連する問題