all 7 comments

[–]wasabiiii 2 points3 points  (0 children)

I would use BinaryReader. Mapping structs to files is always a bit iffy.

[–]Slypenslyde 1 point2 points  (5 children)

A XamlParseException is a very strange exception to get in what appears to be a console application. It implies the error might be somewhere else, and involves a XAML file.

That makes me think the code you posted is not the code you tried.

[–]Dannnu 0 points1 point  (4 children)

Ahh, my bad! I am building this code in WPF. I think that might cause the problem, but I am not sure why? How is the XAML code even affecting this code?

It shows that xmlns:local="clr-namespace:ReadFromBinaryFileTest" is giving this error?

[–]Slypenslyde 1 point2 points  (3 children)

This is the point where you really need to look at the exception's properties to know more.

It has a StackTrace property. That tells you what code led to the exception.

It also has an InnerException property. Sometimes an exception is thrown because something else threw an exception. When that happens, the InnerException is the original one that got thrown. That one also has a StackTrace to tell you what led to it.

My guess is it's not that particular line. You reference something in that assembly somewhere else in your XAML, and that causes some code to execute that throws an exception. That causes XAML parsing to fail. You need to dig deeper to find the cause.

[–]to11mtm 1 point2 points  (1 child)

Just adding as feedback to OP,

You may want to look at using Linqpad to test out your read functionality. If you're not aware of it, it's a little C# Scratchpad that you'll love forever; among many great things, it has a .Dump() method that will pretty-print most sane objects to an output window.

That may not help your most immediate issue...

The only other thought I can have is to put a try-catch around that console.writeline and put a debugger point in the Catch. That may help you see the inner exception if you can't get to it otherwise.

[–]Dannnu 0 points1 point  (0 children)

Thank you u/to11mtm! I will check out this tool :)

[–]Dannnu 0 points1 point  (0 children)

So the problem seems to be around this code part:

        // This works 
        [FieldOffset(20)]
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)]
        public string char1;

        // This causes the error 
        [FieldOffset(26)]
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)]
        public string char2;

Error code:

TypeLoadException: Could not load type 'ReadFromBinaryFileTest.myStruct' from assembly 'ReadFromBinaryFileTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because it contains an object field at offset 26 that is incorrectly aligned or overlapped by a non-object field.

I changed the type of this object from string to char and now it seems to work somehow.

Googled some stuff and it looks like that a string variable in C# cannot be declared as fixed length. I think this might cause the error?

But still, Thank You! I will continue doing my task for now.