kartoos khan
kartoos khan

Reputation: 449

Soap response to C# class always return null values

I am having hard time parsing soap response from web service and converting it to c# object. What ever i do it always returns null elements. Here is my Response.

<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' 
 xmlns:tem='http://tempuri.org/'>
 <soapenv:Header/> <soapenv:Body>
 <tem:Response><tem:Result>
  <RETN>108</RETN><DESC> This is an error</DESC></tem:Result></tem:Response>
   </soapenv:Body>
 </soapenv:Envelope>

This is how I am parsing this response.

var xDoc = XDocument.Parse(response);
var xLoginResult = xDoc.Root.Descendants().FirstOrDefault(d => d.Name.LocalName.Equals("Result"));
var serializer = new XmlSerializer(typeof(Result));
using (var reader = xLoginResult.CreateReader())
{
    var convertedResonse= (Result)serializer.Deserialize(reader);
    // inside this convertedResonse RETN and  DESC is alway null
}

Here is my Result Class

   [XmlRoot(ElementName = "Result", Namespace = "http://tempuri.org/")]
    public class Result
    {
        [XmlElement("RETN")]
        public string RETN { get; set; }
        [XmlElement("DESC")]
        public string DESC { get; set; }
    }

    [XmlRoot(ElementName = "Response", Namespace = "http://tempuri.org/")]
    public class Response
    {
        [XmlElement(ElementName = "Result", Namespace = "http://tempuri.org/")]
        public Result Result { get; set; }
    }

    [XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Body
    {
        [XmlElement(ElementName = "Response", Namespace = "http://tempuri.org/")]
        public Response Response { get; set; }
    }

    [XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Envelope
    {
        [XmlElement(ElementName = "Header", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public string Header { get; set; }
        [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public Body Body { get; set; }
        [XmlAttribute(AttributeName = "soapenv", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Soapenv { get; set; }
        [XmlAttribute(AttributeName = "tem", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Tem { get; set; }
    }

Values inside convertedResonse is always null. Any thoughts will be much appreciated?

Upvotes: 1

Views: 839

Answers (1)

Mohammed Sajid
Mohammed Sajid

Reputation: 4903

For the Xml posted in the question, you don't need to use XmlSerializer, just XDocument by using Linq to Xml is sufficient, like the following code :

1 - Create NameSpace :

XNamespace xn = "http://tempuri.org/";

2 - Adapt the query for XDocument to :

Result result = xDoc
    .Descendants(xn + "Result")
    .Select(x => new Result { DESC = x.Element("DESC").Value, RETN = x.Element("RETN").Value })
    .FirstOrDefault();

Console.WriteLine($"DESC:{result.DESC}, RETN:{result.RETN}");

Result

DESC: This is an error, RETN:108

I hope you find this helpful.

Upvotes: 1

Related Questions