download an XML file Using fileStream

D

Dorsa

HI, Could you please tell me the error in here. I am trying to open an
XML file from a link.


Response.Clear()
Response.Expires = 0
Response.BufferOutput = False
Response.ContentType = "text/xml"
Response.AddHeader("Content-Disposition",
"filename=test.XML")

Dim OStream As FileStream
Dim FileSize As Long
Dim ds As New DataSet()

OStream = New FileStream(Server.MapPath("test.XML"),
FileMode.Open, FileAccess.Read)
ds.ReadXml(OStream)

FileSize = OStream.Length
Dim Buffer(CInt(FileSize)) As Byte

OStream.Read(Buffer, 0, CInt(FileSize))
OStream.Close()

Response.Write(Buffer)
Response.End()
 
V

vMike

Try something like this where pathfile is the path and file name of your
file.

Dim fStream As New System.IO.FileStream(pathfile, IO.FileMode.Open,
IO.FileAccess.Read, IO.FileShare.Read)
Dim b(ctype(fStream.Length,int32)) As Byte
Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/unknown"
Response.AddHeader("Content-Disposition", "attachment;filename=" +
filename)
fStream.Read(b, 0, ctype(fStream.Length,int32))
Response.BinaryWrite(b)
Response.End()
fStream.Close()
fStream = Nothing
 
D

Dorsa

Thanks very much mike But here is another problem. I'm trying to open
up that XML file with word2003. the file that I'm trying to open is a
word file which I had saved it as an XML file. After I download the
file, Word2003 opens up but it can't open the file. when I edit the
downloaded file with .net, I see an extra character at the end of the
document.
</w:wordDocument> "it doesn't show here but it's a rectangle" after
">"
Do you have any suggestion?
thanks
Dorsa
 
V

vMike

Well I am not sure. I have never tried to do what you are doing. I tried it
with an excel file saved as a xml file and it worked fine after downloading
it an openning it with excel. I don't have word 2003 and my version of word
doesn't have an xml feature (or maybe it isn't installed). Seems like you
are getting an extra byte. You might want to repost with your problem or
maybe there is a word group that might have a solution. Good luck . If I
run accross anything then I will post.
 
V

vMike

One other probably obvious thing is have you tried reopenning it with
Word2003 before it is downloaded to see that Word is creating a valid xml
file. I read a few threads about it and it fairly new and there have been
some problems with Word documents which contain grahics and other features.
 
D

Dorsa

yes, there is an extra character at the end. I couldn't find the
reason and I also tried with other Response.ContentType but seems that
it dosen't care what for that value. Do you any idea how I can delete
that character byte at the end after ">" ?
Also, Is there another way for getting my XML file into word? Can you
tell me about why you used binary for output?
Thanks
Dorsa
 
V

vMike

Have you tried just a straight forward file write such as. You might also
want to mess with the ContentType

Dim fi as System.IO.FileInfo = new System.IO.FileInfo(youpathandfilename)

Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/unknown"
Response.AddHeader("Content-Disposition", "attachment;filename=" +
filename)
Response.WriteFile(fi.fullname)
Response.End()

fi = Nothing
 
D

Dorsa

Yes, It worked. But here is the things that I'm not sure about that
and I got stuck and I couldn't find any solution for that. I'm pretty
sure that you can help me.
So right now, that XML file can be opend with word2003. it's only text
in that file but I need to treat it as an XML file. (word2003 saved as
XML)
I need to search and replace some text in it. here is an example. I
need to replace "test" with "test1" in "This is a test". Could you
tell me how I can do that? I'm have tried with fileStream and binary
reading but without any success.
I would be thankful if you could help me with that. I'm using vb.net
Thanks
Ayat
 
V

vMike

Well, if I understand what you are trying to do you would do something like
I have below, but there are better ways to do this if your original word
file is structured but you can give this a try. Typically you would go to a
node and change the node values but this should work.


Dim doc As XmlDocument = New XmlDocument()
doc.Load("yourpath and file name.xml")
dim sb as new stringbuilder(doc.innerxml,doc.innerxml.length + 100)
sb.replace("oldtext","newtext")
doc.innerXML = sb.tostring()
doc.save("yourpath and file name.xml") 'note you may want to use a
different name for testing to see how it works

Good luck.
 
D

Dorsa

Appreciate it Mike. You helped me a lot. I like to way you wrote it
and it works perfectly.I wanted to show you my code and ask your idea
about it.
Please read my code below. It works, but I found out that when there
is a comma in my text (not the text that I'm searching for) it changes
into some strange character. I also read about stringBulider and I
found out that whenever that we use one of the methods, we create a
new string object in the memory which requires a new allocation of
space. Since this search and replace is a daily routine and there are
more than 100 documents in near future, should I be worry about Memory
and related issues?
Thanks


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim result, str As String
Dim xml As New XmlDocument()
Dim oStream As FileStream
Dim fStream As New
System.IO.FileStream(Server.MapPath("test.xml"), IO.FileMode.Open,
IO.FileAccess.Read, IO.FileShare.Read)

Dim byteArray(CType(fStream.Length - 1, Int32)) As Byte
fStream.Read(byteArray, 0, CInt(fStream.Length))

Dim i As Integer

For i = 0 To byteArray.Length - 1
str &= Chr(byteArray(i))
Next

result = Replace(str, "test", "test1")

Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/x-msdownload"
Response.AddHeader("Content-Disposition",
"attachment;filename=" + "test.xml")
Response.Write(result)
fStream.Close()
Response.End()

fStream = Nothing

End Sub
 
V

vMike

I am glad you got it working. You might want to take another look at the
stringbuilder info as regards to memory. As I understand it, any time you
change a string, a new string must be created in memory and the old string's
memory is released. The stringbuilder class using the same memory as long as
the size of the "string" isn't greater that the capacity when it is created.
I have never done any tests so I am not sure which is better to use, but
generally if I have a lot of concatenation and/or replacing I prefer the
stringbuilder versus a string, but if I am doing simple string manipulation
I use the string class.

As far as the strange characters, I am not sure but some characters have
special meaning in xml files so that they have to be escaped or otherwise
changed to work correctly. That my be the case in your situation, but if it
works I would worry about it. I would test it with a lot of different
characters like & and > etc to make sure it works.
Good luck/.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top