Hope you can adapt the below code for your requirement, code below turns an object into xml document and put it on memory stream.
on the click event
Dim currentStream As New MemoryStream()
Dim streamWriter As New StreamWriter(currentStream)
streamWriter.Write(CreateXMLDoc(reportDetail))
streamWriter.Flush()
currentStream.Position = 0
Now the "currentStream" contains xml document content as string, Here "reportDetail" is the object which needed to converted to xml. alter this function as to your requirement.
Public Shared Function CreateXMLDoc(ByVal value As Object) As String
Dim rv As String = ""
Dim xml = New XmlSerializer(value.[GetType]())
Using memoryStream As MemoryStream = New MemoryStream()
Using xmlWriter As XmlTextWriter = New XmlTextWriter(memoryStream, Encoding.UTF8)
xmlWriter.Formatting = Formatting.Indented
xml.Serialize(xmlWriter, value)
rv = Encoding.UTF8.GetString(memoryStream.ToArray())
End Using
End Using
Return rv
End Function
you can use the currentStream variable to manipulate the xml, like sending an email with xml attachment.
You could first save the “Request.InputStream” to check whether the content is right. Besides, you could try to use “Request.Files[0].InputStream” to get the HttpPostedFile. If the issue still exists, you could provide the whole code about how to save the
xml files to Steam.
Dim stream As New StreamReader(Request.Files(0).InputStream)
Dim x As String = stream.ReadToEnd()
Dim xml As String = HttpUtility.UrlDecode(x)
Dim document = New XmlDocument()
document.LoadXml(xml)
The only method I have found effective to obtain anything from the inputstream, is this:
Did the method I provide you above have some issue on your client? Could you show the result of the above method? If the method also has the same issue, could you show how you accept the XML? Besides, you could also take a look at the following similar thread
which shows how to get the xml file string.
' Create a Stream object.
Str = Request.InputStream
' Find number of bytes in stream.
strLen = CInt(Str.Length)
' Create a byte array.
Dim strArr(strLen) As Byte
' Read stream into byte array.
strRead = Str.Read(strArr, 0, strLen)
' Convert byte array to a text string.
For counter = 0 To strLen - 1
strmContents = strmContents & strArr(counter).ToString()
Next counter
Dim textResponse = Decompress(strArr)
Private Shared Function Decompress(data As Byte()) As Byte()
Using compressedStream = New MemoryStream(data)
compressedStream.Seek(0, SeekOrigin.Begin)
Using zipStream = New GZipStream(compressedStream, CompressionMode.Decompress)
Using resultStream = New MemoryStream()
zipStream.CopyTo(resultStream)
Return resultStream.ToArray()
End Using
End Using
End Using
End Function
That function results in the exception:
The magic number in GZip header is not correct. Make sure you are passing in a GZip stream.
Member
301 Points
393 Posts
Loading GZIP InputStream Into XML Doc
Aug 31, 2015 06:19 PM|kevorkian|LINK
I have a page that accepts XML, and I've always loaded it for parsing by simply doing:
However the input stream is coming over as a string of digits, like so:
60631201091083211810111411510511111061344....(many, many more)
At first I thought it was base64, but that isn't the case. How do I decode this?
Contributor
6711 Points
2334 Posts
Re: Loading GZIP InputStream Into XML Doc
Aug 31, 2015 11:08 PM|cnranasinghe|LINK
Hope you can adapt the below code for your requirement, code below turns an object into xml document and put it on memory stream.
on the click event
Now the "currentStream" contains xml document content as string, Here "reportDetail" is the object which needed to converted to xml. alter this function as to your requirement.
you can use the currentStream variable to manipulate the xml, like sending an email with xml attachment.
Star
7980 Points
1586 Posts
Re: Loading GZIP InputStream Into XML Doc
Sep 01, 2015 03:29 AM|Weibo Zhang|LINK
Hi kevorkian,
You could first save the “Request.InputStream” to check whether the content is right. Besides, you could try to use “Request.Files[0].InputStream” to get the HttpPostedFile. If the issue still exists, you could provide the whole code about how to save the xml files to Steam.
https://msdn.microsoft.com/en-us/library/system.web.httppostedfile.inputstream(v=vs.110).aspx
I hope it’s useful to you.
Best Regard,
Weibo Zhang
Member
301 Points
393 Posts
Re: Loading GZIP InputStream Into XML Doc
Sep 02, 2015 10:04 PM|kevorkian|LINK
Here is the post header:
Header Info: Cache-Control=no-cache
Connection=close
Content-Length=413
Content-Type=application/xml
Accept=text/xml, application/xml, text/html, text/plain, */* Accept-Encoding=gzip, deflate, * Accept-Language=en-us, en, * Authorization=Basic Og== Host=myhost.com
User-Agent=Mozilla/4.0 (compatible; MSIE 6.0; Windows 98; Win 9x 4.90; Q312461)
The only method I have found effective to obtain anything from the inputstream, is this:
The contents of strmContents is:
6063120109108321181011141151051111106134494648343210111099111100105110103613485847045563463621310601101011191141119710011595100101991051151051111106213106097112112108105999711610511111095105100625655495248604797112112108105999711610511111095105100621310609911211595971121121081059997116105111110951051006257575748564548485050495560479911211595971121121081059997116105111110951051006213106010010199105115105111110951009711610162504849534548574548506047100101991051151051111109510097116101621310601001019910511510511111095116105109101624956585253584956604710010199105115105111110951161051091016213106010010199105115105111110626860471001019910511510511111062131060115116971161171156251496047115116971161171156213106011211110595114101991011051181011006277604711211110595114101991011051181011006213106011211110595118101114105102105101100627360471121111059511810111410510210510110062131060106111989511610510910195118101114105102105101100627360471061119895116105109101951181011141051021051011006213106047110101119114111971001159510010199105115105111110621310
Star
7980 Points
1586 Posts
Re: Loading GZIP InputStream Into XML Doc
Sep 13, 2015 07:35 AM|Weibo Zhang|LINK
Hi kevorkian,
Did the method I provide you above have some issue on your client? Could you show the result of the above method? If the method also has the same issue, could you show how you accept the XML? Besides, you could also take a look at the following similar thread which shows how to get the xml file string.
http://stackoverflow.com/questions/10049003/retrieve-http-post-request-containing-xml
I hope it’s useful to you.
Best Regards,
Weibo Zhang
Member
301 Points
393 Posts
Re: Loading GZIP InputStream Into XML Doc
Sep 16, 2015 04:54 PM|kevorkian|LINK
Ok, so still can't figure this out. Here is the post header:
Cache-Control=no-cache Connection=close Content-Length=413 Content-Type=application/xml Accept=text/xml, application/xml, text/html, text/plain, */* Accept-Encoding=gzip, deflate, * Accept-Language=en-us, en, * Authorization=Basic Og== Host=myhost.com User-Agent=Mozilla/4.0 (compatible; MSIE 6.0; Windows 98; Win 9x 4.90; Q312461)
Here is how I am attempting to decode the stream:
That function results in the exception:
The magic number in GZip header is not correct. Make sure you are passing in a GZip stream.