473,473 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1372


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Dave Rahardja | last post by:
OK, so I've gotten into a philosophical disagreement with my colleague at work. He is a proponent of the Template Method pattern, i.e.: class foo { public: void bar() { do_bar(); } protected:...
5
by: Chris | last post by:
Hi I have a scenario where I've created another AppDomain to dynamically load a DLL(s) into. In this newly loaded DLL I want to call a static method on a class. The problem arise is that I have...
4
by: daniel.w.gelder | last post by:
I wrote a template class that takes a function prototype and lets you store and call a C-level function, like this: inline string SampleFunction(int, bool) {..} functor<string (int, bool)>...
7
by: greenflame | last post by:
I am trying to make a matrix object. I have given it some properites. I am trying to add a method. When I call the method by Test.showDims(...) I want to only enter one input, that is the method by...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
10
by: Mihai Osian | last post by:
Hi everyone, Given the code below, can anyone tell me: a) Is this normal behaviour ? b) If it is, what is the reason behind it ? I would expect the A::method(int) to be inherited by B. ...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
3
by: allendowney | last post by:
Hi All. In a complex inheritance hierarchy, it is sometimes difficult to find where a method is defined. I thought it might be possible to get this info from the method object itself, but it...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.