470,815 Members | 1,158 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

ReadBinHex method

Hi,

I'm trying to read binary information from an XMLTextReader and to save
it to a .jpg file, using a FileStream. I don't want to use an
XmlDocument because of performance reasons. But the only example code I
could find is:

byte[] binhex = new byte[1000];
do {
binhexlen = reader.ReadBinHex(binhex, 0, 50);
for (int i=0; i < binhexlen; i++) Console.Write(binhex[i]);
} while (reader.Name == "BinHex");

Is there any way to discover the length of the binary data on
beforehand? Or can someone give me a hint on how to complete my code
hereunder?

oPreviewStream = new FileStream(@"c:\temp\temp.jpg",
FileMode.Create, FileAccess.Write);
if (oPreviewStream != null)
{
// oTextReader.Value.Length returns zero in this case, I
suppose because of the binary content.
byte[] byteStream = new byte[oTextReader.Value.Length];
oTextReader.ReadBinHex(byteStream, 0,
oTextReader.Value.Length - 1);
oPreviewStream.Write(byteStream, 0, byteStream.Length);
oPreviewStream.Flush();
oPreviewStream.Close();
}
(This code snippet is not complete and not working).

Thanks in advance for the help. Kind regards,

Karine Bosch

Feb 8 '06 #1
2 1294


Karine Bosch wrote:

I'm trying to read binary information from an XMLTextReader and to save
it to a .jpg file, using a FileStream.
Is there any way to discover the length of the binary data on
beforehand? Or can someone give me a hint on how to complete my code
hereunder?

oPreviewStream = new FileStream(@"c:\temp\temp.jpg",
FileMode.Create, FileAccess.Write);
if (oPreviewStream != null)
{
// oTextReader.Value.Length returns zero in this case, I
suppose because of the binary content.
byte[] byteStream = new byte[oTextReader.Value.Length];
oTextReader.ReadBinHex(byteStream, 0,
oTextReader.Value.Length - 1);
oPreviewStream.Write(byteStream, 0, byteStream.Length);
oPreviewStream.Flush();
oPreviewStream.Close();


Read the bytes into a buffer array and write that to the file stream as
long as needed e.g. pseudo code

byte[] buffer = new byte[1024];
Stream fileStream = new FileStream(@"whatever.jpg",
FileMode.Create, FileAccess.Write);

while (xmlReader.Name == hexElementName && xmlReader.NodeType !=
XmlNodeType.EndElement) {
int length = xmlReader.ReadBinHex(buffer, 0, buffer.Length);
if (length != 0) {
fileStream.Write(buffer, 0, length);
}
}
fileStream.Close();
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Feb 8 '06 #2
Thanks, this works!
Kind regards,
Karine Bosch

Martin Honnen schreef:
Karine Bosch wrote:

I'm trying to read binary information from an XMLTextReader and to save
it to a .jpg file, using a FileStream.
Is there any way to discover the length of the binary data on
beforehand? Or can someone give me a hint on how to complete my code
hereunder?

oPreviewStream = new FileStream(@"c:\temp\temp.jpg",
FileMode.Create, FileAccess.Write);
if (oPreviewStream != null)
{
// oTextReader.Value.Length returns zero in this case, I
suppose because of the binary content.
byte[] byteStream = new byte[oTextReader.Value.Length];
oTextReader.ReadBinHex(byteStream, 0,
oTextReader.Value.Length - 1);
oPreviewStream.Write(byteStream, 0, byteStream.Length);
oPreviewStream.Flush();
oPreviewStream.Close();


Read the bytes into a buffer array and write that to the file stream as
long as needed e.g. pseudo code

byte[] buffer = new byte[1024];
Stream fileStream = new FileStream(@"whatever.jpg",
FileMode.Create, FileAccess.Write);

while (xmlReader.Name == hexElementName && xmlReader.NodeType !=
XmlNodeType.EndElement) {
int length = xmlReader.ReadBinHex(buffer, 0, buffer.Length);
if (length != 0) {
fileStream.Write(buffer, 0, length);
}
}
fileStream.Close();
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/


Feb 20 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Dave Rahardja | last post: by
4 posts views Thread by daniel.w.gelder | last post: by
7 posts views Thread by greenflame | last post: by
5 posts views Thread by Nick Flandry | last post: by
10 posts views Thread by Mihai Osian | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.