Nikita Goncharuk
Nikita Goncharuk

Reputation: 815

Deserialize Soap response C#

I have some problems with the deserialization of my Soap response, i get an exception when try to deserialize with XmlSerializer class

Soap Data:

HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <IsWmsLoginCorrectResponse xmlns="http://pehade.be/">
      <IsWmsLoginCorrectResult>
        <UserId>int</UserId>
        <LoginToken>guid</LoginToken>
        <ErrorId>int</ErrorId>
        <ErrorDescription>string</ErrorDescription>
      </IsWmsLoginCorrectResult>
    </IsWmsLoginCorrectResponse>
  </soap12:Body>
</soap12:Envelope>

TokenInfo model:

public const string DefaultSoapNamespace = "http://pehade.be/";

//...

[XmlRoot("IsWmsLoginCorrectResponse", Namespace = Consts.DefaultSoapNamespace)]
    public class TokenInfo : BaseSoapInfo
    {
        [XmlAttribute(AttributeName = "UserId", Namespace = Consts.DefaultSoapNamespace)]
        public int UserId { get; set; }

        [XmlAttribute(AttributeName = "LoginToken", Namespace = Consts.DefaultSoapNamespace)]
        public string LoginToken { get; set; }
    }

public class BaseSoapInfo
    {
        [XmlAttribute(AttributeName = "ErrorId", Namespace = Consts.DefaultSoapNamespace)]
        public int ErrorId { get; set; }

        [XmlAttribute(AttributeName = "ErrorDescription", Namespace = Consts.DefaultSoapNamespace)]
        public string ErrorDescription { get; set; }
    }

Parse Method:

public override T ParseSoap<T>(string resultSoap)
        {
            var document = XDocument.Parse(resultSoap);
            var xmlSerializer = new XmlSerializer(typeof(T));

            var soapBody = XName.Get("Body", Consts.DefaultSoapNamespace);

// Exception
            var tokenInfo = (T)xmlSerializer.Deserialize(document.Descendants(soapBody)
                .First()
                .FirstNode
                .CreateReader());         

            return tokenInfo;
        }

Did I make the model correctly? Can there is other method to receive stream for XmlSerializer?

Update: Add exception details

System.InvalidOperationException: Sequence contains no elements at System.Linq.Enumerable.First[TSource] (System.Collections.Generic.IEnumerable`1[T] source) [0x00010] in :0 at DAL.SoapConverters.LoginSoapConverter.ParseSoap[T] (System.String resultSoap) [0x00031] in C:\Work\Projects\Apro\Other MDO\WarehouseMS\DAL\SoapConverters\LoginSoapConverter.cs:40 08-30 14:56:01.858 I/mono-stdout( 4702): System.InvalidOperationException: Sequence contains no elements

Upvotes: 1

Views: 4740

Answers (1)

Mac
Mac

Reputation: 957

I think that the XmlAttribute(s) should be changed to XmlElement. For instance the UserId should be:

[XmlElement(Namespace = Consts.DefaultSoapNamespace)]
public int UserId { get; set; }

Upvotes: 3

Related Questions