I have this program that creates a XML file to the disk drive. I then pick up the file and I send it to a web service that gives me a response, which is a xml file in memory and I write the response as a XML file to the disk drive.
There is no need for me to keep the XML Files on the drive, and I'd rather create the file in memory, and pass it to the server.
This is what I want to do
XmlRequest = CreateXML() 'Create XML File in memory
XmlResponse = TransmitXML(XML) 'Transit the Xml Memory file , and then take the XML Response
ParseXML(XmlResponse)
Any Quick Tips would really help out
Thanks in advance
Private Function CreateXML() As Boolean
Dim Context As HttpContext = HttpContext.Current
Dim strPath As String
Dim objWriter As XmlTextWriter 'create an instance of the XmlTextWriter object
Try
' location to the XML file to write
strPath = Context.Server.MapPath("ECommerce.xml")
objWriter = New XmlTextWriter(strPath, System.Text.Encoding.UTF8)
objWriter.Formatting = Formatting.Indented
objWriter.WriteStartDocument()
'objWriter.WriteComment("Write the XML File Version 1.0")
'Write the Main Element RequestData
objWriter.WriteStartElement("RequestData")
objWriter.WriteAttributeString("action", "Request")
objWriter.WriteAttributeString("version", "1.1")
'Write the Requestor Element
objWriter.WriteStartElement("Requestor")
objWriter.WriteElementString("ID", Requestor_ID)
objWriter.WriteElementString("Password", Requestor_Password)
objWriter.WriteEndElement()
'#################################################################################################################
'Shipment
objWriter.WriteStartElement("Shipment")
objWriter.WriteAttributeString("action", "RateEstimate")
objWriter.WriteAttributeString("version", "1.0")
'Shipment/ShippingCredentials
objWriter.WriteStartElement("ShippingCredentials")
objWriter.WriteElementString("ShippingKey", Shipment_ShippingCredentials_ShippingKey)
objWriter.WriteElementString("AccountNbr", Shipment_ShippingCredentials_AccountNbr)
objWriter.WriteEndElement()
'Shipment/ShipmentDetail
objWriter.WriteStartElement("ShipmentDetail")
objWriter.WriteElementString("ShipDate", ShipmentDetail_ShipDate)
'End of Shipment Element
objWriter.WriteEndElement()
'########################################################################################################
'End of Request Element
objWriter.WriteEndElement()
objWriter.Flush()
objWriter.Close()
Return True
Catch ex As Exception
Context.Response.Write("Create XML Error" & ex.Message & "< br />")
End Try
objWriter = Nothing
End Function Private Sub TransmitXML(ByVal XML As MemoryStream)
Dim Context As HttpContext = HttpContext.Current
Dim sr As StreamReader = New StreamReader(Context.Server.MapPath("ECommerce.xml"))
Dim sw As StreamWriter
Dim XMLRead As String = sr.ReadToEnd
Dim stXML As String
stXML = XMLRead
sr.Close()
Try
Dim results As String
Dim request As WebRequest = WebRequest.Create("https://www.request.com/request.asp")
request.Method = "POST"
request.ContentType = "text/xml; charset=utf-8"
results = WriteToURL(request, stXML)
Dim Response As String
Response = RetrieveFromURL(request)
sw = File.CreateText(Context.Server.MapPath("ECommerceReply.xml"))
'sw.WriteLine(Response)
sw.Write(Response)
sw.Flush()
sw.Close()
sw = Nothing
sr = Nothing
Catch ex As Exception
Context.Response.Write("Error Message from DHL:" & ex.Message & "<br />")
Exit Sub
End Try
End Sub