'Deserialize XML / SOAP Response RestSharp

I am trying to deserialize a SOAP response using RestSharp.

The response is:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
        <ns2:processDataResponse xmlns:ns2="http://somelink/">
            <return>8616</return>
        </ns2:processDataResponse>
    </S:Body>
</S:Envelope>

These are my XML Elements:

   [XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Envelope
    {
        [XmlElement(ElementName = "Body")]
        public Body body { get; set; }
    }
    public class Body
    {
        [XmlElement(ElementName = "processDataResponse", Namespace = "http://somelink/")]
        public ProcessDataResponse ProcessDataResponse { get; set; }
    }
    public class ProcessDataResponse
    {
        [XmlElement(ElementName = "return")]
        public string ret { get; set; }
    }

And this is the code for deserialization:

            IRestResponse response = client.Execute(request);
            Console.WriteLine(response.Content);

            XmlAttributeDeserializer deserializer = new XmlAttributeDeserializer();
            
            var result = deserializer.Deserialize<Envelope>(response);

            Console.WriteLine(result.body.ProcessDataResponse.ret);

I just get no output.

What am I doing wrong? Or is there something spezial about the "S:Envelope" and "ns:processDataResponse"?

Thanks!

EDIT:

I've made a simple test using the XMLSerializer, one test which includes the namespaces in the XML, and one without the namespaces.

Without namespaces everything works as expected, with Namespaces, there is no value returned.

This is the whole code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace xmltest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string testxml_with_namespace = @"<S:Envelope xmlns:S=""http://schemas.xmlsoap.org/soap/envelope/"">
                            <S:Body>
                                <ns2:processDataResponse xmlns:ns2=""http://somelink/"">
                                    <return>8616</return>
                                </ns2:processDataResponse>
                            </S:Body>
                        </S:Envelope>
                        ";

        string testxml_without_namespace = @"<Envelope>
                            <Body>
                                <processDataResponse>
                                    <return>8616</return>
                                </processDataResponse>
                            </Body>
                        </Envelope>
                        ";

        private void Button1_Click(object sender, EventArgs e)
        {

            XmlSerializer serializer = new XmlSerializer(typeof(Envelope));

            TextReader reader = new StringReader(testxml_with_namespace);
            Envelope env = (Envelope)serializer.Deserialize(reader);

            Console.WriteLine("With Namespace return=" + env.body.ProcessDataResponse.ret);

            reader.Close();

            XmlSerializer serializer2 = new XmlSerializer(typeof(Envelope2));

            TextReader reader2 = new StringReader(testxml_without_namespace);
            Envelope2 env2 = (Envelope2)serializer2.Deserialize(reader2);

            Console.WriteLine("Without Namespace return=" + env2.body.ProcessDataResponse.ret);

            reader2.Close();
        }

        //WITH NAMESPACE

        [XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public class Envelope
        {
            [XmlElement(ElementName = "Body")]
            public Body body { get; set; }
        }
        public class Body
        {
            [XmlElement(ElementName = "processDataResponse", Namespace = "http://somelink/")]
            public ProcessDataResponse ProcessDataResponse { get; set; }
        }
        public class ProcessDataResponse
        {
            [XmlElement(ElementName = "return")]
            public string ret { get; set; }
        }

        //WITHOUT NAMESPACE

        [XmlRoot(ElementName = "Envelope")]
        public class Envelope2
        {
            [XmlElement(ElementName = "Body")]
            public Body2 body { get; set; }
        }
        public class Body2
        {
            [XmlElement(ElementName = "processDataResponse")]
            public ProcessDataResponse2 ProcessDataResponse { get; set; }
        }
        public class ProcessDataResponse2
        {
            [XmlElement(ElementName = "return")]
            public string ret { get; set; }
        }


    }
}

And the output:

With Namespace return= 
Without Namespace return=8616

Why is this failing, if I use namespaces in the XML?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source