1

I'm making some test to use it.

I have the following xml:

<?xml version="1.0"?>
<test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <ma>233</ma>
    <ma>2333</ma>
</test>

I have this class to deserialize it:

[Serializable]
public class test
{
    public string ma { get; set; }
}

It does contains the first element. Now I want both so I try setting an array

[Serializable]
public class test
{
    public string[] ma { get; set; }
}

However setting an array I have now 0 result in ma variable, while I at least have the first one when it is not an array.

I found this answer Using XmlSerializer with an array in the root element, but he used another logic... I'd like to keep using [Serializable]

Community
  • 1
  • 1

2 Answers2

1

The answer you found provides the information you need. [Serializable] doesn't help you because it isn't used by XmlSerializer, see Why doesn't the XmlSerializer need the type to be marked [Serializable]?

Community
  • 1
  • 1
candidus
  • 119
  • 6
1

You have to indicate that the array doesn't have a separate xml element to wrap its items, but that the array items appear directly under the <test> element:

public class test
{
    [XmlElement]
    public string[] ma { get; set; }
}

PS. sometimes it's hard to get the mapping right - I usually fill in a class with test data and serilalize it, examining what XmlSerializer makes of that usually clears up what's going on.

C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72