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]

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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]?

Comments

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.