how to send xml file

J

jens d

Hi NG,

I'm trying to send a XML file. The receiver should be able to work with
the content without saving it as a file. (Best with XMLReader, but that
will be part of further postings ;) )

I tried it the following way, but it doesnt work out.
Please have a look at my code:
Sender:

long s_length = FileStream.Length;
long s_read = 0;

StreamWriter sw = new StreamWriter(NetworkStream, Encoding.UTF8, 1024);
using (StreamReader sr=new StreamReader(FileStream, Encoding.UTF8,true))
{
while (s_length > s_read)
{
char[] buffer = new char[1024];
int read = sr.Read(buffer, 0, 1024);
s_read += read;
sw.Write(buffer,0,read);
}
}
NetworkStream.Flush();

One Problem with the sender is that the StreamWriter doesnt seem to send
the buffer every time when sw.Write(..) is called.
Another, that the calculated size (s_read) is not the same as
FileStream.lenth and therefore this ends up in a 'do_nothing_loop' :(
Suggestions??

Receiver:

using (StreamReader sr = new StreamReader(NetworkStream, ENCODING.UTF8))
{
while(true)
{
char[] buffer = new char[1024];
int read = sr.Read(buffer, 0, 1024);
StringBuilder.Append(buffer);
if (read == 0)
break;
}
}

Within the StringBuilder I hoped to find the complete XML-file, but I
only got the first 1024bytes as it seems.
Does anybody see a mistake?
Do you know a better way to send the file?
Somebody done this before? -> Links, Tutorials?

Thanks a lot

Jens
 
G

Guest

Have you tried calling sw.Flush(), after you call sw.Write()?? This should
clear out any internal buffers and write them to the stream.
 
J

Jens D

Great Thanks. Now the whole Stream arrives at the Receiver. Another
Problem occured though:

Receiver:

while(true)
{
char[] buffer = new char[1024];
int read = sr.Read(buffer, 0, 1024);
StringBuilder.Append(buffer);
if (read == 0)
break;
}

after receiving the whole Stream, the Loop will hang at sr.Read() and
won't terminate.
Suggestions?
Thanx!!

Jens
 
M

Marc Gravell

I'm not entirely sure, but I would refactor this as below; two reasons:

1: could read be -ve?
2: the final read could return a partial buffer (e.g. 47 bytes); you should
then only add those 47 to the string builder

int read = st.Read(buffer, 0, 1024);
if(read>0) {
// append *only what you read*
}
else { // EOF
break;
}

Marc
 
N

Nick Hounsome

Jens D said:
Great Thanks. Now the whole Stream arrives at the Receiver. Another
Problem occured though:

Receiver:

while(true)
{
char[] buffer = new char[1024];

move the buffer outside the loop.
int read = sr.Read(buffer, 0, 1024);
StringBuilder.Append(buffer);

check "read" before Append.

You are using the wrong Append(buffer) = Append(buffer.ToString() =
Append("char[]") or something similar.

You want Append(buffer,0,read)
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,
StreamWriter sw = new StreamWriter(NetworkStream, Encoding.UTF8, 1024);

Why are you reading it as a UTF8 if you need to convert it again to byte[] ?
, just read it as a binary file ( using FileStream )
NetworkStream.Flush();

IIRC you do not need to call Flush in a networkstream , yep I'm correct,
check MSDN

I would rewrite the sender like:
long s_length = FileStream.Length;

networkStream.Write( BitConverter.GetBytes( s_length), 0, 8 ); //send the
size
using (FileStream sr=new FileStream(FileStream) )
{
while (s_length > s_read)
{
byte[] buffer = new byte[1024];
int read = sr.Read(buffer, 0, 1024);
s_read += read;
networkStream.Write(buffer,0,read);
}
}

The receiver is kind of similar, read the size, then create a memorystream
with that size and read that amount of bytes directly to the memorystream,
you can decorate the memorystream with a XmlTextReader
 
J

Jens D

Thanx Ignacio,
the sender works fine.

On the side of the receiver the size of the xml-file seems to be larger.
So I won't get the whole stream when using (s_length > s_read) condition.
How come?

Thanks again.
Hi,
Sender:

StreamWriter sw = new StreamWriter(NetworkStream, Encoding.UTF8, 1024);


Why are you reading it as a UTF8 if you need to convert it again to byte[] ?
, just read it as a binary file ( using FileStream )

NetworkStream.Flush();


IIRC you do not need to call Flush in a networkstream , yep I'm correct,
check MSDN

I would rewrite the sender like:
long s_length = FileStream.Length;

networkStream.Write( BitConverter.GetBytes( s_length), 0, 8 ); //send the
size
using (FileStream sr=new FileStream(FileStream) )
{
while (s_length > s_read)
{
byte[] buffer = new byte[1024];
int read = sr.Read(buffer, 0, 1024);
s_read += read;
networkStream.Write(buffer,0,read);
}
}

The receiver is kind of similar, read the size, then create a memorystream
with that size and read that amount of bytes directly to the memorystream,
you can decorate the memorystream with a XmlTextReader
 
J

Jens D

Hi again,

actually the receiver is not working fine. The MemoryStream does not
seem to contain any of the data.
Do I handle the Stream wrong?
Thanks again

Receiver:


MemoryStream ms = new MemoryStream();
while(true)
{
buffer = new byte[buffersize];
read = NetworkStream.Read(buffer,0,buffersize);
if(read==0)
break;
ms.Write(buffer,0, buffersize);
}
 

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

Top