3

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

3
  • 1
    Have you tried serializing a List<byte> Commented Sep 2, 2011 at 11:33
  • I get an Error when I try to serialize a List. I don't think that is possible. "There was an error reflecting type'...' " SORRY: I missed to delete the DataType Xml Tag. It works! Commented Sep 2, 2011 at 11:37
  • List<byte> is exactly what I was looking for. Thanks the_ajp. Commented Sep 2, 2011 at 11:40

2 Answers 2

4
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>
Sign up to request clarification or add additional context in comments.

Comments

0

I guess you'd have to explicitly go create a new XML element for each byte in the array. You might as well use a List<byte> or Array<byte> for doing that, instead of a raw array.

As the_ajp said in the comment.

If you're getting an error trying to serialize the list, you've done something wrong. Post the code, perhaps we can help.

2 Comments

I mentioned that you said it :)
Ye thanks, I already commented above, I've missed to delete the DataType = heyBinary part.

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.