我正在尝试使用Visual Studio将XML响应文本加载到Windows窗体应用程序中的HTML对象中,但是会引发错误
对象引用未设置为对象的实例
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim XMLReq As MSXML2.XMLHTTP60 = New MSXML2.XMLHTTP60
Dim HTMLDoc As mshtml.HTMLDocument = New mshtml.HTMLDocument
Dim Table As MSHTML.IHTMLElement
URL = "https://tt.wiki.com/search?"
XMLReq.open("GET", URL, False)
XMLReq.send()
If XMLReq.status <> 200 Then
MsgBox("Error" & vbNewLine & XMLReq.status & " - " & XMLReq.statusText)
Exit Sub
End If
Dim wb As WebBrowser = New WebBrowser
HTMLDoc = wb.Document.DomDocument
HTMLDoc.body.innerHTML = XMLReq.responseText //object reference error occuring here//
Table = HTMLDoc.getElementById("search_results")
End sub
该错误发生在行:
HTMLDoc.body.innerHTML = XMLReq.responseText
答案 0 :(得分:0)
经过这么多次尝试,终于找到了答案。 需要在访问其body.innerHTML方法之前向HTMLDoc对象分配一些HTML代码。 下面是代码....它对我来说很好`
Dim XMLReq As MSXML2.XMLHTTP60 = New MSXML2.XMLHTTP60
Dim HTMLDoc As mshtml.HTMLDocument = New mshtml.HTMLDocument
Dim HTMLDoc1 As mshtml.IHTMLDocument = HTMLDoc
Dim Table As MSHTML.IHTMLElement
URL = "https://tt.wiki.com/search?"
XMLReq.open("GET", URL, False)
XMLReq.send()
If XMLReq.status <> 200 Then
MsgBox("Error" & vbNewLine & XMLReq.status & " - " & XMLReq.statusText)
Exit Sub
End If
HTMLDoc1.write("<html><body>test</body></html>")
HTMLDoc1.close()
HTMLDoc = HTMLDoc1
HTMLDoc.body.innerHTML = XMLReq.responseText
Table = HTMLDoc.getElementById("search_results")`