0

I have a soap xml message and need to fetch a single node value from given soap xml

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://tews6/wsdl" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.xmlsoap.org/soap/envelope/ http://schemas.xmlsoap.org/soap/envelope/" >
<soapenv:Body>
<testResult>
<Status version="6.0" >
<NNO>277982b4-2917a65f-13ffb8c0-b09751f</NNO>
</Status>
<ProfileTab>
<Email>abc@gmail.com</Email>
<Name>abc</Name>
</Profile>
</testResult></soapenv:Body></soapenv:Envelope>

I need to fetch the value of Email node. I used the below code

 rootNode = "soapenv:Envelope/soapenv:Body/ProfileTab/Email";

 var nsmgr = new XmlNamespaceManager(document.NameTable);
 nsmgr.AddNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");
 nsmgr.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");

 node = document.SelectSingleNode(rootNode,nsmgr);

It is returning the null.

  • check this answer http://stackoverflow.com/questions/6442024/getting-specified-node-values-from-xml-document – M.Azad Mar 05 '15 at 05:12
  • Are you sure your XML is valid `Profile` dosn't have matching tag. It has `ProfileTab` is that a typo ? – Mahesh Mar 05 '15 at 05:31

2 Answers2

0

You can use the following.

var rootNode = "soapenv:Envelope/soapenv:Body/tews6:testResult/tews6:ProfileTab/tews6:Email";

var nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("tews6", "http://tews6/wsdl");
nsmgr.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");

var node = doc.SelectSingleNode(rootNode, nsmgr);
Parthasarathy
  • 2,698
  • 1
  • 12
  • 14
0

Try this:

string xml="xml"; 
XDocument doc = XDocument.Parse(xml);
 XNamespace bodyNameSpace ="http://schemas.xmlsoap.org/soap/envelope/";
 var bodyXml = from _e in doc.Descendants(bodyNameSpace + "Body")
                          select _e;
 if (bodyXml.Elements().Count() == 0)
            {
                return;
            }
var email = from _e in bodyXml.First()Descendants("Email")
                                      select _e;
if(email.Count()==1)
{
    string emailAddress=email.First().Value;
}
malkam
  • 2,337
  • 1
  • 14
  • 17