Reputation: 139
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="https://webservice.someWebService.php">
<SOAP-ENV:Body>
<ns1:loginResponse>
<return>
<errorCode>OK</errorCode>
<header>
<sessionToken>tokentokentokentokentokentokentokentokentoken</sessionToken>
<errorCode>OK</errorCode>
</header>
</return>
</ns1:loginResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
I am trying to get the sessionToken from the XML above and place it into a string variable.
This is the code that i have tried:
string soapmessage = response.Content;
XmlDocument document = new XmlDocument();
document.LoadXml(soapmessage); //loading soap message as string
XmlNamespaceManager manager = new XmlNamespaceManager(document.NameTable);
manager.AddNamespace("d", "http://someURL");
manager.AddNamespace("bhr", "https://webservice.someWebService.php");
XmlNodeList xnList = document.SelectNodes("//bhr:loginResponse", manager);
int nodes = xnList.Count;
string Status;
foreach (XmlNode xn in xnList)
{
Status = xn["d:sessionToken"].InnerText;
}
But im getting an error of
System.NullReferenceException: 'Object reference not set to an instance of an object.'
Upvotes: 0
Views: 439
Reputation: 497
I believe you want to do this:
string Status = xnList[0]["return"]["header"]["sessionToken"].InnerText;
The sessionToken is not directly under the first node, so attempting to access it will return null
, and calling .InnerText
on null
will result in NullReferenceException.
Upvotes: 1