Solved

TextWriter not writing everything

Posted on 2004-04-21
9
1,154 Views
Last Modified: 2007-12-19
I have a webservice that takes a base64 encoded string and converts it to xml, and then writes it out to a file.

Here is the code that creates the base64 string:

MemoryStream memStream = new MemoryStream();
                        TextWriter writer = new StreamWriter(memStream);
                        TextReader reader = new StreamReader(this.txtPath.Text);
                        writer.Write(reader.ReadToEnd());
                        writer.Close();
                        param = Convert.ToBase64String(memStream.ToArray();


And here is the code that reads the base64 string back to text (xml):

public string Add(string Base64Binary)
            {
                  try
                  {
                        MemoryStream memStream = new MemoryStream(Convert.FromBase64String(Base64Binary));
                        TextReader reader = new StreamReader(memStream);
                        string temp = reader.ReadToEnd();
                        FileStream strm = new FileStream(Server.MapPath(Guid.NewGuid().ToString() + ".xml"), FileMode.Create, FileAccess.Write);
                        TextWriter writer = new StreamWriter(strm);
                        writer.Write(temp);
                        strm.Close();
                        return "Ack";
                  }

The thing is: The file, when I open it, seems to be truncated! Have any of you guys run into this problem before, and know what's going on? Thanks!
0
Comment
Question by:gregasm
  • 5
  • 4
9 Comments
 
LVL 12

Expert Comment

by:dfiala13
ID: 10925186
Need to flush the writer. It's onoly writing the first 1024 characters.

TextWriter writer = new StreamWriter(strm);
 writer.Write(temp);
writer.Flush();
 strm.Close();


0
 
LVL 12

Accepted Solution

by:
dfiala13 earned 500 total points
ID: 10925214
closing the writer should work too (it should flush first).  In the original code you close the underlying stream instead of the writer, which is why it truncated.
0
 
LVL 8

Author Comment

by:gregasm
ID: 10925221
I am gonna try this first thing tomorrow morning.. THANKS MAN! I have a good feeling about this one hehehe
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 12

Expert Comment

by:dfiala13
ID: 10925225
You should. I actually tested the code for once. ;)
0
 
LVL 8

Author Comment

by:gregasm
ID: 10930417
Thank you! I knew it was some "mundane detail" (from office space). =]]
0
 
LVL 12

Expert Comment

by:dfiala13
ID: 10930749
Proper use of "flair" will often overcome those "mundane details".
0
 
LVL 8

Author Comment

by:gregasm
ID: 10930787
Hahah, dfiala13 you're a cool guy. =]]
0
 
LVL 8

Author Comment

by:gregasm
ID: 10930792
and smart! 153003 pts in one month! ! dang. you've got lots of flair. =]
0
 
LVL 12

Expert Comment

by:dfiala13
ID: 10930856
blush, blush
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

806 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question