Solved

TextWriter not writing everything

Posted on 2004-04-21
9
1,151 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
 
LVL 12

Expert Comment

by:dfiala13
ID: 10925225
You should. I actually tested the code for once. ;)
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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 …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now