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

后端 未结 3 1266
野性不改
野性不改 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:53

    This is how I did it:

    public class MySerializableClass
    {
        private string dummy;
    
        [XmlElement("NaughtyXmlCharacters")]
        public string NaughtyXmlCharactersAsString
        {
           get 
           {
               return BitConverter.ToString(NaughtyXmlCharacters);
           }
           set
           {
               // without this, the property is not serialized.
               dummy = value;
           }
        }
    
        [XmlIgnore]
        public byte[] NaughtyXmlCharacters
        {
            get;
            set;
        }
    }
    

    The bytes are then formatted as hexadecimal values and separated with a minus sign: 00-AF-B1

提交回复
热议问题