Solved

TextWriter not writing everything

Posted on 2004-04-21
9
1,155 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

Suggested Solutions

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…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

839 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