Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# byte array xml serialization

I got a byte array

public byte[] values;

I fill it with data

new byte[64];

I serialize it and I get the following XML part:

<values>
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==
</values>

I found the following solution here in SO:

[XmlElement("values", DataType = "hexBinary")]
        public byte[] values;

Now I get the same XML as above just with "0" instead of "A".

When I serialize e.g. a Int16/Int32/sbyte array. I get something like this in XML:

<values>0</values>
<values>0</values>
<values>0</values>

In a vertical arrangement.

Now my question: Is it possible to get a byte array also in a vertical arrangement? Like:

    <values>00</values>
    <values>00</values>
    <values>00</values>

Mark

like image 437
Mark Avatar asked Apr 08 '26 16:04

Mark


1 Answers

public class Test
{
    public List<byte> Bytes { get; set; }
}

var xml = new XmlSerializer(typeof(Test));
xml.Serialize(File.Open("test.xml",FileMode.OpenOrCreate),
              new Test
              {
                  Bytes = new List<byte> {0,1,2,3,4,5,6,7}
              });

results in a xml file like:

<?xml version="1.0"?>
<Test xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Bytes>
    <unsignedByte>0</unsignedByte>
    <unsignedByte>1</unsignedByte>
    <unsignedByte>2</unsignedByte>
    <unsignedByte>3</unsignedByte>
    <unsignedByte>4</unsignedByte>
    <unsignedByte>5</unsignedByte>
    <unsignedByte>6</unsignedByte>
    <unsignedByte>7</unsignedByte>
  </Bytes> 
</Test>
like image 128
albertjan Avatar answered Apr 11 '26 04:04

albertjan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!