TextWriter not writing everything

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!
LVL 8
gregasmAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
dfiala13Connect With a Mentor Commented:
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
 
dfiala13Commented:
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
 
gregasmAuthor Commented:
I am gonna try this first thing tomorrow morning.. THANKS MAN! I have a good feeling about this one hehehe
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
dfiala13Commented:
You should. I actually tested the code for once. ;)
0
 
gregasmAuthor Commented:
Thank you! I knew it was some "mundane detail" (from office space). =]]
0
 
dfiala13Commented:
Proper use of "flair" will often overcome those "mundane details".
0
 
gregasmAuthor Commented:
Hahah, dfiala13 you're a cool guy. =]]
0
 
gregasmAuthor Commented:
and smart! 153003 pts in one month! ! dang. you've got lots of flair. =]
0
 
dfiala13Commented:
blush, blush
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.