[thelist] ASP.NET Posting an xml file?

Brian Cummiskey Brian at hondaswap.com
Mon Sep 25 11:45:41 CDT 2006


Casey,

I've recently built an address verification tool via UPS.  I used 
Classic ASP however, so my code will obviously need to be changed.

Basically what you need to do is built a string of XML to post.
I used the following:

dim strXML
strXML = ""
        strXML = strXML & "<?xml version=""1.0""?>"
        strXML = strXML & "<AccessRequest xml:lang=""en-US"">"
        strXML = strXML & "    
<AccessLicenseNumber>xxxxxxxxxxxxxxxxxxx</AccessLicenseNumber>"
        strXML = strXML & "    <UserId>FOO</UserId>"
        strXML = strXML & "    <Password>BAR</Password>"
        strXML = strXML & "</AccessRequest>"
        strXML = strXML & "<?xml version=""1.0""?>"
        strXML = strXML & "<AddressValidationRequest xml:lang=""en-US"">"
        strXML = strXML & "    <Request>"
        strXML = strXML & "        <TransactionReference>"
        strXML = strXML & "            
<CustomerContext>TC1000201</CustomerContext>"
        strXML = strXML & "            <XpciVersion>1.0002</XpciVersion>"
        strXML = strXML & "        </TransactionReference>"
        strXML = strXML & "        <RequestAction>XAV</RequestAction>"
        strXML = strXML & "    </Request>"
        strXML = strXML & "    <AddressKeyFormat>"
        strXML = strXML & "        <ConsigneeName>"& firstname & " " & 
lastname &"</ConsigneeName>"
        strXML = strXML & "        <AddressLine>"& address1 & 
"</AddressLine>"
        strXML = strXML & "        <PoliticalDivision2>"& city & 
"</PoliticalDivision2>"
        strXML = strXML & "        <PoliticalDivision1>"& state & 
"</PoliticalDivision1>"
        strXML = strXML & "        <PostcodePrimaryLow>"& zip & 
"</PostcodePrimaryLow>"
        strXML = strXML & "        <CountryCode>" & country 
&"</CountryCode>"
        strXML = strXML & "    </AddressKeyFormat>"
        strXML = strXML & "<MaximumListSize>10</MaximumListSize>"
        strXML = strXML & "</AddressValidationRequest>"


Then, send the post:

            Set objXmlHttp = CreateObject("MSXML2.ServerXMLHTTP.3.0")
            objXmlHttp.setTimeouts toResolve, toConnect, toSend, toReceive
            objXmlHttp.Open "POST", baseURL, False
            objXMLHttp.Send(strXML)

Then fetch the response from UPS:

        returnedxml = objXMLHttp.responseXML.XML
       'Instantiate XML parser and load XML file
            Set objDoc = Server.CreateObject("MSXML2.DOMDocument.3.0")
            objDoc.async = False
            If Not objDoc.loadXML(returnedxml) Then
                'Response.Write "Failed to load XML"
                Response.Redirect "review.asp"
            End If




        'XML ERROR CHECK
            For Each objNode1 In objDoc.selectNodes("//Response")        
       
                For Each objNode2 In objNode1.ChildNodes
                    Select Case objNode2.nodeName
                        Case "ResponseStatusCode"
                            If Server.HTMLEncode(objNode2.text) <> "1" Then
                                'response.write "XML ERROR!"
                                Response.Redirect "review.asp"
                            Else
                                statustext = "Success"
                            End If                       
                    End select
                Next
            Next



            If statustext = "Success" Then


show the result table here:

        response.write "<div id=""avsresults"">" & vbcrlf
                                response.write "<table><tr><td 
valign=""top"" width=""40%"">" & vbcrlf
                                Response.Write "<p>The following 
suggestions were returned:</p>" & vbcrlf
                                response.write "<table>" & vbcrlf


                                'Walk through the XML nodes
                                For Each objNode1 In 
objDoc.selectNodes("//AddressKeyFormat")
                                    Response.Write "<tr><td class=""wrap"">"
                                        For Each objNode2 In 
objNode1.ChildNodes
                                            Select Case objNode2.nodeName
                                                Case 
"PostcodeExtendedLow" ' don't show zip+4....
                                                    ' do nothing
                                                Case Else
                                                    Response.Write 
Server.HTMLEncode(objNode2.text) & "<br />" & vbcrlf
                                            End select
                                        Next
                                    Response.Write "</td></tr>" & vbcrlf
                                Next
                                response.write "</table></td><td 
valign=""top"" width=""60%"">" & vbcrlf

and so on....

                'Clean up
                Set objHTTP = Nothing
                Set objDoc = Nothing
                Set objNode1 = Nothing
                Set objNode2 = Nothing


I have next to zero .net experience, so i won't be much help there.    
but hopefully this will get you started.



More information about the thelist mailing list