0

I am reading data from a binary files with the purpose of converting it to xml. For this i have class with all the marshaling defined to read it.

The text values are as 32byte fixed length strings - in ANSI korean codepage.

I use XmlSerializer Serialize() to save it as xml.

    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
    public byte[] pName;

However XmlSerializer only supports base64/hex with byte[].

I cannot use

UnmanagedType.ByValTStr

Because it does not allow specifying codepage and i get incorrect, corrupt strings like:

µðÆúÆ®º§¶óÅä³²ÀÚÀå°©1

How can i get the data to read as EUC-KR string or provide a custom serialization for these specific 32 byte arrays to convert it to readable format myself?

In total i am dealing with ~20 files, each with different structure - but usig same 32 byte strings for text.

So manual conversions and looping through nested data with various class structures is not a viable option.

UPDATE: example struct:

   [StructLayout(LayoutKind.Sequential)]
    public struct ClientData
    {
        [MarshalAs(UnmanagedType.U4)]
        public uint index;
        [MarshalAs(UnmanagedType.U4)]
        public uint serial;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string pName;
        public string StrName { get { return System.Text.Encoding.GetEncoding("EUC-KR").GetString(pName, 0, 32); } }

    }
7
  • 1
    What does MarshalAs have to do with XML serialisation? Commented Jul 24, 2018 at 10:32
  • Unclear what you want to write to your xml file... Xml files (normally) are saved in UTF-8 or UTF-16, so support the whole Unicode. Do you want to save your binary data as (korean) text or you want to save it as binary data? Because in an xml binary data is saved as hex or base64. Commented Jul 24, 2018 at 10:51
  • @mjwills - data is loaded from binary with Marchal and then serialized as xml. So ether getting correct string when Marshaling the data OR when serializing - ether approach would be fine if it works. Commented Jul 24, 2018 at 11:04
  • @xanatos All of it, the entire struct will be converted into xml - but the text values end up in a unreadable format right now. It will have mixed data but those 32 byte ansi korean text values should be human readable when i serialize the object. Commented Jul 24, 2018 at 11:04
  • @Agony Then put a public string strName { get { return Encoding.GetEncoding("EUC-KR").GetString(pName); } }, or do the GetString manually. The point is that you must have a string property for the XmlSerializer to save it as text. Commented Jul 24, 2018 at 11:08

1 Answer 1

1

Based on the comment I wrote, use something like:

public class MyClass
{
    private static readonly Encoding koreanEncoding = Encoding.GetEncoding("EUC-KR");

    [XmlIgnore]
    public byte[] pName;

    public string pNameString
    {
        get => koreanEncoding.GetString(pName).TrimEnd('\0');
        set
        {
            var temp = koreanEncoding.GetBytes(value);
            Array.Resize(ref temp, 32);
            pName = temp;
        }
    }
}

So create a proxy pNameString that transform pName and use [XmlIgnore] to remove it from the xml. XmlSerializer probably requires both a get and a set in a property to be serialized.

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

2 Comments

Added shorter example struct in the main thread. When i use Serialize() on it - the field is not added to the xml.
Seems like setter was mandatory even if you were just serializing. Also in the example above - temp is undefined.

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.