在将XML响应文本解析为HTMLDoc.body.innerHTML时,如何在VB.net中修复“对象引用未设置为对象的实例”

时间:2018-12-31 06:50:34

标签: vb.net nullreferenceexception

我正在尝试使用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

1 个答案:

答案 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")`