How to serialize byte array to XML using XmlSerializer in C#?

后端 未结 3 1270
野性不改
野性不改 2021-01-04 07:05

Say we have a struct that it\'s data is provided by un-managed byte array using Marshal.PtrToStructure.

The C# struct layout:

[StructLayout(LayoutKin         


        
3条回答
  •  鱼传尺愫
    2021-01-04 07:31

    The XmlSerializer by default will encode the byte arrays using base 64 encoding. If you use this site and paste in IzE=, and decode it, the result will be #1. You can change the encoding by setting the XmlElementAttribute.DataType. I'm not sure if [XmlElement(DataType = "string")] will work, but you can try it. Using [XmlElement(DataType = "hexBinary")] will generate the raw bytes.

提交回复
热议问题