I'm trying to parse a SOAP message into a specific Class, but I'm having trouble.
This is the SOAP Message:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<LoginResponse
xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<LoginResult>
<CookieName>FedAuth</CookieName>
<ErrorCode>NoError</ErrorCode>
<TimeoutSeconds>1800</TimeoutSeconds>
</LoginResult>
</LoginResponse>
</soap:Body>
</soap:Envelope>
I have a simple class with 3 attributes:
public class SoapResponse
{
public string CookieName { get; set; }
public int TimeoutSeconds { get; set; }
public string ErrorCode { get; set; }
}
I'm trying to Use Linq to evaluate the Soap XML and parse it into a an object of the SoapResponse Class. So far I have the next code:
var xml = XDocument.Parse(responseXml);
var soapResponse = from result in xml.Descendants(XName.Get("LoginResult", xmlNamespace))
let cookieNameElement = result.Element(XName.Get("CookieName", xmlNamespace)) where cookieNameElement != null
let timoutSecondsElement = result.Element(XName.Get("TimoutSeconds", xmlNamespace)) where timoutSecondsElement != null
let errorCodeElement = result.Element(XName.Get("ErrorCode", xmlNamespace)) where errorCodeElement != null
select new SoapResponse
{
CookieName = cookieNameElement.Value,
TimeoutSeconds = Convert.ToInt32(timoutSecondsElement.Value),
ErrorCode = errorCodeElement.Value
};
I know that this is a very similar post to this Using LINQ to XML to Parse a SOAP message post, but I can't find a way to work it around.
Thanks in advance! :)
Try code below. I removed soap from first tag.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication102
{
class Program
{
static void Main(string[] args)
{
string responseXml =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<Envelope" +
" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
"<soap:Body>" +
"<LoginResponse" +
" xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\">" +
"<LoginResult>" +
"<CookieName>FedAuth</CookieName>" +
"<ErrorCode>NoError</ErrorCode>" +
"<TimeoutSeconds>1800</TimeoutSeconds>" +
"</LoginResult>" +
"</LoginResponse>" +
"</soap:Body>" +
"</Envelope>";
XDocument xml = XDocument.Parse(responseXml);
var soapResponse = xml.Descendants().Where(x => x.Name.LocalName == "LoginResult").Select(x => new SoapResponse()
{
CookieName = (string)x.Element(x.Name.Namespace + "CookieName"),
TimeoutSeconds = (int)x.Element(x.Name.Namespace + "TimeoutSeconds"),
ErrorCode = (string)x.Element(x.Name.Namespace + "ErrorCode")
}).FirstOrDefault();
}
}
public class SoapResponse
{
public string CookieName { get; set;}
public int TimeoutSeconds { get; set;}
public string ErrorCode { get; set;}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With