473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Dissappearing text in Byte[] conversion

Ian
I am creating an XML file through the XmlTextWriter. This is output to a
MemoryStream which I convert a string through a Byte Array. Everything works
correctly except for one BIG issue. My XML file is being truncated somewhere
in the process. Large XML files give a truncated result, and small ones rsult
in a Byte[].Length = 0 .

I assume the it is getting stuck in a buffer??
I tried Fluch() on the MemStream and Base Stream without success.

Code Fragment:
MemoryStream memStream = new MemoryStream();
xmlw = new XmlTextWriter(memStream,Encoding.UTF8);
//
//Code that creates the XML Document
//xmlw.BaseStream.Flush();
//memStream.Flush();

memStream.Position = 0;
Byte[] info = new byte[memStream.Length];
int li = memStream.Read(info,0,(int)memStream.Length);
int lo = info.Length;
return new System.Text.UTF8Encoding().GetString(info,0,info.L ength);

Nov 17 '05 #1
5 2299
Ian <Ia*@discussions.microsoft.com> wrote:
I am creating an XML file through the XmlTextWriter. This is output to a
MemoryStream which I convert a string through a Byte Array. Everything works
correctly except for one BIG issue. My XML file is being truncated somewhere
in the process. Large XML files give a truncated result, and small ones rsult
in a Byte[].Length = 0 .

I assume the it is getting stuck in a buffer??
I tried Fluch() on the MemStream and Base Stream without success.

Code Fragment:
MemoryStream memStream = new MemoryStream();
xmlw = new XmlTextWriter(memStream,Encoding.UTF8);
//
//Code that creates the XML Document
//xmlw.BaseStream.Flush();
//memStream.Flush();

memStream.Position = 0;
Byte[] info = new byte[memStream.Length];
int li = memStream.Read(info,0,(int)memStream.Length);
int lo = info.Length;
return new System.Text.UTF8Encoding().GetString(info,0,info.L ength);


It's not the base stream you need to flush - it's the XmlTextWriter.
You should close that, and then use ToArray on the MemoryStream.

Having said all that - why don't you just use a StringWriter in the
first place?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
Ian
I was using a StringWriter and it worked great except for the fact that it
would default the XML tag to UTF-16. When I would attempt to open this file
in my transform after saving it to disk I would get an encoding error.

According to class documentation IO classes that having a underlying string
default to UTF-16 and you can't change this...well I couldn't get a UTF-8 tag
instead.

any suggestions?


It's not the base stream you need to flush - it's the XmlTextWriter.
You should close that, and then use ToArray on the MemoryStream.

Having said all that - why don't you just use a StringWriter in the
first place?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #3
Ian <Ia*@discussions.microsoft.com> wrote:
I was using a StringWriter and it worked great except for the fact that it
would default the XML tag to UTF-16.


There's an easy solution to that:

public class StringWriterWithEncoding : StringWriter
{
Encoding encoding;

public StringWriterWithEncoding (Encoding encoding)
{
this.encoding = encoding;
}

public override Encoding Encoding
{
get { return encoding; }
}

}

Create a StringWriterWithEncoding with the UTF-8 encoding, and write to
that instead - it should be fine.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
Ian

I have tried setting the Encoding property before, but I always get "
REad-Only Property" error.

I did the following and got the same result for this.Encoding = encoding;
public class StringWriterUTF8:System.IO.StringWriter
{
//System.Text.Encoding encoding; << what is this for?
public StringWriterUTF8(System.Text.Encoding encoding)
{
this.Encoding = encoding;
}
public override System.Text.Encoding Encoding
{
get
{
return encoding;
}
}

}

"Jon Skeet [C# MVP]" wrote:


Create a StringWriterWithEncoding with the UTF-8 encoding, and write to
that instead - it should be fine.


Nov 17 '05 #5
Ian <Ia*@discussions.microsoft.com> wrote:
I have tried setting the Encoding property before, but I always get "
REad-Only Property" error.
Yes, that's why I provided a class which derives from StringWriter.
I did the following and got the same result for this.Encoding = encoding;


You don't need to set the Encoding property - you provide the value
when you construct the instance.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Nikolaos Giannopoulos | last post by:
I have the following page: http://solmar.ca/temp6/index.html On IE5 and IE6 win the "solmar" logo dissappears when the window is made smaller horizontally. Any idea how to fix this problem?...
2
by: Mark | last post by:
Sorry about the last... Anyway, here's the question: I've been working on some C# routines to process strings in and out of various encodings. The hope is that I can just let the user type in...
40
by: Peter Row | last post by:
Hi all, Here is my problem: I have a SQL Server 2000 DB with various NVarChar, NText fields in its tables. For some stupid reason the data was inserted into these fields in UTF8 encoding. ...
36
by: Wei Su | last post by:
Hi, I have a text file abc.txt and it looks like: 12 34 56 23 45 56 33 56 78 ... .. .. ... .. .. I want to get how many rows totally in the text file, how to do this? Thanks.
1
by: Wasim Akram | last post by:
Hi, I have a field "Month" in my SQL server table. The type of this field is "tinyint". Now what I am doing in the code is using DataRow to read this field in a 'int' variable. int month...
4
by: I.Charitopoulos | last post by:
The reason I want to do so, is that I am sending to DOS and I am pretty certain that it will not work. Everything I've tried so far hasnt. In my test environment (Windows to Windows) this works...
10
by: Nikolay Petrov | last post by:
How can I convert DOS cyrillic text to Unicode
4
by: George | last post by:
Hi, I am puzzled by the following and seeking some assistance to help me understand what happened. I have very limited encoding knowledge. Our SAP system writes out a text file which includes...
6
by: i_robot73 | last post by:
I have a file, containing hex values for dates (MMDDYYYY)<status code><??such as: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.