B
bbb
Hi,
I need to convert XML files from Japanese encoding to UTF-8.
I was using the following code:
using ( FileStream fs = File.OpenRead(fromFile) )
{
int fileSize = (int)fs.Length;
int buffer = fileSize;
byte[] b = new byte[buffer];
using(StreamWriter sw = new StreamWriter(toFile, true, toEnc))
{
while (fs.Read(b,0,buffer) > 0)
{
byte[] utf8Bytes = Encoding.Convert(fromEnc, toEnc, b);
// Convert the new byte[] into a char[] and then into a string.
char[] utf8Chars = new char[toEnc.GetCharCount(utf8Bytes, 0,
utf8Bytes.Length)];
toEnc.GetChars(utf8Bytes, 0, utf8Bytes.Length, utf8Chars,
0);
string utfString = new string(utf8Chars);
sw.Write(replaceXmlEncodingHeader(utfString, fromEncHeader
,toEncHeader));
}
}
}
Everything worked fine until we get 100MB file - I got OutOfMemory
exception.
I've tried to read it by pieces :
if (fileSize >30000000)
{
buffer = 1024;
}
but then it fills the end of the buffer with other bytes (lets say last
chunk 900 bytes - it adds 124 bytes from somewhere else) -so my
converted xml is not well formed.
Please help.
Thanks in advance.
Regards
I need to convert XML files from Japanese encoding to UTF-8.
I was using the following code:
using ( FileStream fs = File.OpenRead(fromFile) )
{
int fileSize = (int)fs.Length;
int buffer = fileSize;
byte[] b = new byte[buffer];
using(StreamWriter sw = new StreamWriter(toFile, true, toEnc))
{
while (fs.Read(b,0,buffer) > 0)
{
byte[] utf8Bytes = Encoding.Convert(fromEnc, toEnc, b);
// Convert the new byte[] into a char[] and then into a string.
char[] utf8Chars = new char[toEnc.GetCharCount(utf8Bytes, 0,
utf8Bytes.Length)];
toEnc.GetChars(utf8Bytes, 0, utf8Bytes.Length, utf8Chars,
0);
string utfString = new string(utf8Chars);
sw.Write(replaceXmlEncodingHeader(utfString, fromEncHeader
,toEncHeader));
}
}
}
Everything worked fine until we get 100MB file - I got OutOfMemory
exception.
I've tried to read it by pieces :
if (fileSize >30000000)
{
buffer = 1024;
}
but then it fills the end of the buffer with other bytes (lets say last
chunk 900 bytes - it adds 124 bytes from somewhere else) -so my
converted xml is not well formed.
Please help.
Thanks in advance.
Regards