I know it's question about C#, but since it's the first question that is coming in the google, I will answer for Java.
In my case, I was using Jackson2XmlMessageConverter.
After I added
@XmlRootElement(name = "root", namespace = "http://namespace")
to my root element, I had same issue for the children.
Thanks to the accepted answer, I understood that I had to annotate fields with
@XmlElement(namespace = "http://namespace")
(and do it recursively for their children) and it resolved the issue.
Example test:
class MyMessageParserTest {
// initialize the converter in your preferred way: Autowire, manually in @BeforeEach, ...
private Jackson2XmlMessageConverter converter;
private static MyMessage createMyMessage(){
// create your object here
}
private static String createRawXml() {
// create expected xml
}
@Test
public void fromObjectToXml_OK() {
//given
String expectedXml = createRawXml();
MyMessage object = createMyMessage();
MessageProperties messageProperties = new MessageProperties();
messageProperties.setContentType("application/xml");
Message message = converter.toMessage(object, messageProperties);
//when
String result = new String(message.getBody(), Charset.defaultCharset());
//then
assertThat(result).isXmlEqualTo(expectedXml); //might be deprecated,
}
@Test
public void fromXmlToObject_OK() {
//given
MyMessage expectedObject = createMyMessage();
String rawXml = createRawXml();
MessageProperties messageProperties = new MessageProperties();
messageProperties.setContentType("application/xml");
Message message = new Message(rawXml.getBytes(), messageProperties);
//when
MyMessage result = (MyMessage) converter.fromMessage(message, new ParameterizedTypeReference<MyMessage>() {
});
//then
assertEquals(expectedObject, result);
}
}