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

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

    I got this to work using the following:

      public class MySerializableClass
      {   
        [XmlIgnore]
        public string NaughtyXmlCharactersAsString { get; set; }
    
        [XmlElement(ElementName = "NaughtyXmlCharacters", DataType = "hexBinary")]
        public byte[] NaughtyXmlCharactersAsBytes
        {
            get { return Encoding.UTF8.GetBytes(NaughtyCharactersAsString ?? string.Empty); }
            set { NaughtyXmlCharactersAsString = Encoding.UTF8.GetString(value); }
        }
    

    I would then only access the "AsString" version of the property.

提交回复
热议问题