Create XML File in memory, pass to function

Last post 12-16-2007 1:46 PM by jkirkerx. 3 replies.

Sort Posts:

  • Create XML File in memory, pass to function

    12-11-2007, 4:10 PM
    • Participant
      1,747 point Participant
    • jkirkerx
    • Member since 12-07-2007, 7:52 AM
    • Huntington Beach CA
    • Posts 406

     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
      
     
    Filed under:
  • Re: Create XML File in memory, pass to function

    12-11-2007, 7:04 PM
    Answer
    • All-Star
      21,646 point All-Star
    • gunteman
    • Member since 07-11-2007, 12:57 PM
    • Norrköping, Sweden
    • Posts 3,176

    Dim writer as New StringWriter()
    Dim objWriter As New XmlTextWriter( writer )

    ..... 


    Return writer.ToString()

     

    The response part seems to be OK already. Just return the string variable "Response", and skip the file creation part. 

    -- "Mark As Answer" if my reply helped you --
  • Re: Create XML File in memory, pass to function

    12-12-2007, 12:59 PM
    • Participant
      1,747 point Participant
    • jkirkerx
    • Member since 12-07-2007, 7:52 AM
    • Huntington Beach CA
    • Posts 406
      I finally figured out how to make this work up to the Italics. I can create my xml file info,
    hold it memory and pass it to the TransmitRateRequestStream(XMLRequest) and get back
    a qualified anwser.
    But in TransmitRateRequestStream(), I get a response back called Response which is a string.
     
    I want to take that Response, and pass it to ParseXML in the same format I submitted to
    TransmitRateRequestStream(XMLRequest), so I can parse it as a legit XML File.
    Any Ideas?
    Dim XMLRequest As IO.MemoryStream = Nothing
    XMLRequest = CreateXML()
    
    Dim XMLResponse As IO.MemoryStream = Nothing
    XMLResponse = TransmitRateRequestStream(XMLRequest)
    
    Dim RateInfo(99) As String
    RateInfo = ParseXML(XMLResponse)
     
    Private Function TransmitRateRequestStream(ByVal XMLRequest As IO.MemoryStream) As IO.MemoryStream
    
    		Dim Context As HttpContext = HttpContext.Current
    
    		'Read and process the XMLRequest Stream
    		Dim XMLResponse As IO.MemoryStream = Nothing
    		XMLRequest.Position = 0
    		Dim sr As StreamReader = New StreamReader(XMLRequest)
    
    		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://ecommerce.url/Test.asp")
    			request.Method = "POST"
    			request.ContentType = "text/xml; charset=utf-8"
    			results = WriteToURL(request, stXML)
    
    			Dim Response As String
    			Response = RetrieveFromURL(request)
    			
    			
    
    		Catch ex As Exception
    			Context.Response.Write("Error Message from DHL:" & ex.Message & "<br />")
    
    		End Try
    
    		Return Writer
    
    	End Function
    	Private Function WriteToURL(ByVal Request As WebRequest, ByVal data As String) As String
    
    		Dim Context As HttpContext = HttpContext.Current
    
    		Try
    			Dim bytes = System.Text.Encoding.ASCII.GetBytes(data)
    			Request.ContentLength = bytes.Length
    
    			Dim OutPutStream As Stream = Request.GetRequestStream
    			OutPutStream.Write(bytes, 0, bytes.length)
    			OutPutStream.Close()
    
    		Catch ex As Exception
    			Context.Response.Write("Write to URL Error:" & ex.Message & "< br />")
    
    		End Try
    
    		Return Nothing
    
    	End Function
    	Private Function RetrieveFromURL(ByVal Request As WebRequest) As String
    
    		Dim Context As HttpContext = HttpContext.Current
    
    		Try
    			Dim response As WebResponse = Request.GetResponse
    			Dim stream As Stream = response.GetResponseStream
    			Dim sr As StreamReader = New StreamReader(stream)
    			Return sr.ReadToEnd
    
    		Catch ex As Exception
    			Context.Response.Write("Received Error From URL" & ex.Message & "< br />")
    
    		End Try
    
    		Return Nothing
    
    	End Function
     
  • Re: Create XML File in memory, pass to function

    12-16-2007, 1:46 PM
    Answer
    • Participant
      1,747 point Participant
    • jkirkerx
    • Member since 12-07-2007, 7:52 AM
    • Huntington Beach CA
    • Posts 406

    I finally figured this out through process of elimination and hard work.   These are the functions that I called, and passed the xml memory files from.

    Public Function GetRate()  As Array
    	Dim XMLRequest As IO.MemoryStream = Nothing
    	XMLRequest = CreateXML()
    
    	Dim XMLResponse As IO.MemoryStream = Nothing
    	XMLResponse = TransmitRateRequestStream(XMLRequest)
    
    	Dim RateInfo(99) As String
    	RateInfo = ParseXMLResponse(XMLResponse)
    End Function

     When I created the XML request, this is how I prepared the xml file object in CreateXML()

    Private Function CreateXML() As IO.MemoryStream
    Dim objWriter As XmlTextWriter
    Dim writer As New IO.MemoryStream
    objWriter = New XmlTextWriter(writer, System.Text.Encoding.UTF8)
    
    
    objWriter.WriteStartDocument()
    'create yoru xml content
    'Finish your content
    
    objWriter.WriteEndElement()
    objWriter.WriteEndDocument()
    
    objWriter.Flush()
    writer.Position = 0
    Return writer '	'As IO.MemoryStream
    End Function

    Now we are in TransmitRateRequestStream()

    Private Function TransmitRateRequestStream(ByVal XMLRequest As IO.MemoryStream) As IO.MemoryStream
    
    Dim Context As HttpContext = HttpContext.Current
    
    'Read and process the XMLRequest Stream
    Dim XMLResponse As IO.MemoryStream = Nothing
    XMLRequest.Position = 0
    
    Dim sr As StreamReader = New StreamReader(XMLRequest)
    		
    Dim objWriter As XmlTextWriter
    Dim writer As New IO.MemoryStream
    
    Dim Response As String = Nothing
    
    Dim XMLRead As String = sr.ReadToEnd
    Dim stXML As String
    stXML = XMLRead
    sr.Close()
    
    'Transmit your request to whatever server
    'Receive back the response from the server
    'This was the hardest part, I kept getting illegal character errors when I injected the response in write raw, until I made that utf8 endcoding. 
    There was no need to filter or clean the response  of bad characters
    
    'Create a new XML file or memory Stream
    Dim utf8 As Encoding = New UTF8Encoding()
    objWriter = New XmlTextWriter(writer, utf8)
    objWriter.WriteRaw(Response)
    objWriter.Flush()
    writer.Position = 0
    Return writer ' As IO.memoryStream
    End Function
     

     

     

Page 1 of 1 (4 items)