To read a message from SOAP in C#, we can use the XDocument class provided by "System.Xml.Linq". The other namespaces are "System.Xml" and "System.Xml.Serialization". Here is a custom code snippet where the first parameter is the SOAP response and the second parameter is the node name which you want to read.
- public static string GetSoapMessage(string soapBody, string nodeName)
- {
- XNamespace soapNameSpace
- = XNamespace.Get("YOUR SOAP NAMESPACE URL");
- var document = XDocument.Parse(soapBody);
-
- var soapMessage = document?.Root?.Descendants()?.Where(p =>
- p.Name.LocalName.Equals(nodeName) && p.Name.Namespace
- == soapNameSpace).FirstOrDefault()?.ToString();
-
- soapMessage = soapMessage?.Replace("xmlns:urn=", "xmlns=")
- .Replace("<urn:", "<")
- .Replace("</urn:", "</");
-
- return soapMessage;
- }