1

I have a binary file that contains xmldata in bytes 2-43. How would I go about extracting that data to a file?

I am able to extract small integer fields doing something like this:

Row.TenderNumber = BitConverter.ToInt16(Row.RawBytesraw, 44);

However, I don't know how to extract xml data from this file. Any help is appreciated.

user1389739
  • 111
  • 1
  • 2
  • 15

2 Answers2

1

Something like this ought to work

    using (var stream = new MemoryStream(<byte[] here>))
    using (var reader = new StreamReader(stream))
    {
        var buffer = new char[41];
        stream.Seek(<offset where string begins>, SeekOrigin.Begin);
        reader.Read(buffer, 0, 41);
        <mystringVariable> = new string(buffer);
    }

I wrote it in C# but you get the idea.

Ani
  • 10,826
  • 3
  • 27
  • 46
  • So in your example, how would I take bytes 2-43 from Row.RawBytesraw and output that to a variable? – user1389739 Dec 12 '12 at 16:13
  • actually, I mean if you have the array of bytes that only contains bytes 2 - 43, you can just return new StreamReader(new MemoryStream(myBytes)).ReadToEnd(); -- this will autodetect the encoding etc -- now that will only be a string of course -- optionally you could just skip the string and pass the memory stream into your xml parser -- or you could keep the string and pass it into your xml parser instead. -- Just a matter of choice. – BrainSlugs83 Jun 05 '13 at 23:56
1

xml data is just text with correct formatting. If you can extract bytes from file, you can convert them into string. And string can be XML.

Have a look on this question: binary file to string

Community
  • 1
  • 1
trailmax
  • 34,305
  • 22
  • 140
  • 234
  • I don't understand what you linked. I mentioned it was a file, but it is actually coming from a database. When I attempted to implement what was in your link, it was just blank. – user1389739 Dec 12 '12 at 16:02