Solved

TextWriter not writing everything

Posted on 2004-04-21
9
1,160 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

627 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