When I try to parse: I get the following error message:
Unable to cast COM object of type
'Microsoft.SqlServer.MSXML6.DOMDocumentClass' to interface type
'MSXML2.ServerXMLHTTP40'. This operation failed because the QueryInterface
call on the COM component for the interface with IID
'{2E01311B-C322-4B0A-BD77-B90CFDC8DCE7}' failed due to the following error:
No such interface supported (Exception from HRESULT: 0x80004002
(E_NOINTERFACE))."}
This is my code:
Private Sub cmdLogin_Click()
Dim loginXml As String
Dim loginResponse As MSXML2.DOMDocument40
loginXml = "<?xml version='1.0' encoding='ISO-8859-1'?>" & _
"<Login xmlns=""urn:schemas-tms:Login"">" & _
"<CustomerName>Test Closet</CustomerName>" & _
"<UserName>test</UserName>" & _
"<Password>none</Password>" & _
"</Login>"
loginResponse = SendXmlRequest(loginXml)
If Not loginResponse Is Nothing Then
If ParseLoginResponse(loginResponse) Then
cmdQueryAssetList.Enabled = True
End If
Else
Console.WriteLine("Login request failed.")
End If
End Sub
Private Function SendXmlRequest(ByRef xml As String) As MSXML2.DOMDocument40
Dim xmpRequest As MSXML2.ServerXMLHTTP40
xmpRequest = New MSXML2.ServerXMLHTTP40
' Set known timeout values so we have more control over our request.
Call xmpRequest.setTimeouts(10000&, 10000&, 30000&, 30000&)
' Open the Xmp Url for a Post operation and make the call synchronously.
Call xmpRequest.open("POST", XmpUrl, False)
' Set the necessary Http headers.
Call xmpRequest.setRequestHeader("Accept", "text/html, text/plain")
Call xmpRequest.setRequestHeader("User-Agent", "Custom App Name v1.0")
Call xmpRequest.setRequestHeader("Content-Type", "text/xml")
Call xmpRequest.send(xml)
' If the request times out, it throws an error, but we can simply check
the readyState
On Error Resume Next
If xmpRequest.readyState = 4 Then
' Turn error handling back on.
On Error GoTo 0
' The Http request was completed. However, this does not mean
' that it was necessarily successful. A successful Http response
' will have a Status Code of 200.
If xmpRequest.Status = 200 Then
' The Http request/response was successful, now we
' need to check the Xml that was returned for any errors.
If Not RequestHasErrors(xmpRequest.responseText) Then
' The overall request was successful so we can begin
' to parse the response Xml as necessary.
SendXmlRequest = xmpRequest.responseXML
End If
Else
' There was an Http error.
Console.WriteLine("Status Code: " & xmpRequest.Status)
Console.WriteLine("Status Text: " & xmpRequest.statusText)
Console.WriteLine("Response Text: " & xmpRequest.responseText)
End If
Else
Call MsgBox("There was an error. ReadyState = " &
xmpRequest.readyState)
End If
End Function
Private Function RequestHasErrors(ByRef xml As String) As Boolean
' Check for the high level error Xml in any response before
' trying to parse the results.
If InStr(xml, "<Response><Error>") >= 1 Then
RequestHasErrors = True
End If
End Function
Private Function ParseLoginResponse(ByRef loginDom As MSXML2.DOMDocument40)
As Boolean
' See if the login was successful. If so, get the SessionID.
' Otherwise, log the error.
ParseLoginResponse = False
' Setup the Dom to use XPath queries and also setup
' an Xml namespace prefix for use in the XPath queries.
Call loginDom.setProperty("SelectionLanguage", "XPath")
Call loginDom.setProperty("SelectionNamespaces",
"xmlns:ns='urn:schemas-tms:LoginResponse'")
Dim node As MSXML2.IXMLDOMNode
node = loginDom.selectSingleNode("ns:LoginResponse/ns:Status")
If node.Text = "Success" Then
' The login was 100% successful so get the SessionID.
node = loginDom.selectSingleNode("ns:LoginResponse/ns:SessionID")
g_SessionID = node.Text
ParseLoginResponse = True
Else
' The login was not 100% successful.
node = loginDom.selectSingleNode("ns:LoginResponse/ns:SystemMessage")
Console.WriteLine("Login unsuccessful: " & node.Text)
End If
loginDom = Nothing
node = Nothing
End Function
Private Sub cmdQueryAssetList_Click()
Dim queryAssetListXml As String
Dim queryAssetListResponse As MSXML2.DOMDocument40
queryAssetListXml = "<?xml version='1.0' encoding='ISO-8859-1'?>" & _
"<QueryAssetList2
xmlns=""urn:schemas-tms:QueryAssetList2"">" & _
"<SessionID>" & g_SessionID & "</SessionID>" & _
"</QueryAssetList2>"
queryAssetListResponse = SendXmlRequest(queryAssetListXml)
If Not queryAssetListResponse Is Nothing Then
Call ParseQueryAssetListResponse(queryAssetListResponse )
Else
Console.WriteLine("Query Asset List failed.")
End If
End Sub
Private Sub ParseQueryAssetListResponse(ByRef assetListDom As
MSXML2.DOMDocument40)
' Setup the Dom to use XPath queries and also setup
' an Xml namespace prefix for use in the XPath queries.
Call assetListDom.setProperty("SelectionLanguage", "XPath")
Call assetListDom.setProperty("SelectionNamespaces",
"xmlns:ns='urn:schemas-tms:QueryAssetList2'")
Console.WriteLine(assetListDom.xml)
Dim organizations As MSXML2.IXMLDOMNodeList
Dim org As MSXML2.IXMLDOMNode
Dim assets As MSXML2.IXMLDOMNodeList
Dim asset As MSXML2.IXMLDOMNode
organizations =
assetListDom.selectNodes("ns:QueryAssetList2/ns:Organization")
For Each org In organizations
Console.WriteLine(org.Attributes.getNamedItem("Nam e").Text)
assets = org.selectNodes("ns:AssetList/ns:Asset")
For Each asset In assets
Console.WriteLine(" " &
asset.Attributes.getNamedItem("ID").Text)
Next
Next
End Sub