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.

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

So in your example, how would I take bytes 2-43 from Row.RawBytesraw and output that to a variable?
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.
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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.