Parse a soap XML to a C# class
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! :)
c# xml linq soap
add a comment |
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! :)
c# xml linq soap
add a comment |
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! :)
c# xml linq soap
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! :)
c# xml linq soap
c# xml linq soap
edited May 23 '17 at 12:31
Community♦
11
11
asked Jun 21 '16 at 16:06
metal-gogometal-gogo
639
639
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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;}
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37949435%2fparse-a-soap-xml-to-a-c-sharp-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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;}
}
}
add a comment |
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;}
}
}
add a comment |
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;}
}
}
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;}
}
}
answered Jun 21 '16 at 16:29
jdwengjdweng
17.7k2817
17.7k2817
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37949435%2fparse-a-soap-xml-to-a-c-sharp-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown