Search code examples
asp.net.netxmlvb.netmsxml

Sample Code: VB getNamedItem XML attribute in Microsoft MSXML.DOMDocument


Apologies, I know this is olden days stuff but I'm suffering a bit of brain fade here, I just need a specific code example to get on...

This is the problem using an XML snippet returnd by a remote service

<pap>
  <push-response push-id="99c2d0d3-6cce-4207-88c4-54bc8e852501">
    <address address-value="WIN">5557489580</address>
    <response-result code="1000"/>
  </push-response>
</pap>

I wrote a simple VB function years ago that extracts the data I want from the XML

<%@ Page Language="VB" explicit="true" validateRequest="false" %>
<%@ Import namespace="ADODB" %>
<%@ Import namespace="WinHttp" %>
<%@ Import namespace="MSXML" %>
<%@ Import namespace="Scripting" %>

<script language="VB" runat="Server">

Function ExtractDataFromXML2(ByRef sInputXML As String) As String
Dim XMLDom As MSXML.DOMDocument
Dim currNode As MSXML.IXMLDOMNodeList
Dim Node As MSXML.IXMLDOMNode
Dim sError As String
Dim sMessageID As String
Dim sResult As String

XMLDom = New MSXML.DOMDocument
XMLDom.async = False
If (XMLDom.loadXML(sInputXML) = False) Then
    sResult = "XML Parse Error: " & XMLDom.parseError.reason & " code=" & XMLDom.parseError.errorCode & " " & Chr(13) & Chr(10)
Else
    currNode = XMLDom.selectNodes("//push-response")
    For Each Node In currNode
        sError = Node.selectSingleNode("response-result").attributes.getNamedItem("code").Text
        sMessageID = "rubbish"
        If sError = "1000" Then
            sResult = Node.selectSingleNode("address").Text & "@" & sMessageID
        Else
            sResult = "Error " & sError & ": " & Node.selectSingleNode("response-result").attributes.getNamedItem("desc").Text          
        End If
    Next Node
End If
ExtractDataFromXML2 = sResult
End Function

</script>

For the life of me, I can't remember how to get that GUID push-id into my sMessageID currently initialised "rubbish"

Works fine otherwise... just ONE LINE of VB required, please..?


Solution

  • It should be.

    sMessageID = Node.attributes.getNamedItem("push-id").Text