Click here to Skip to main content
15,441,629 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Soap response as:
XML
<?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://example.com/SystemIntegration">
  			<FirstName>@FirstName</FirstName>
			<LastName>@LastName</LastName>
		</LoginResponse>
    </soap:Body>
 </soap:Envelope>


I'm trying to read it as:
C#
XDocument doc = XDocument.Parse(strReturnStatus);
List<XElement> result = doc.Elements("LoginResponse").ToList();
for (int intc = 0; intc <= result.Count - 1; intc++)
{
    strResponseCode = result[intc].Element("FirstName").Value.ToString();
    strResponseText = result[intc].Element("LastName").Value.ToString();
}


But it returning null result.

How to read above respose in asp.net c#??
Posted
Updated 20-Jun-19 4:40am
Comments
Afzaal Ahmad Zeeshan 4-Feb-16 8:48am    
What is null, response itself or the element?

First problem: The Elements method only selects direct children of the specified node. You need to use the Descendants method instead.

Second problem: The node you're trying to select is not called "LoginResponse"; it has a default namespace, which is part of the element name. You need to include that namespace when retrieving the elements.
C#
XDocument doc = XDocument.Parse(strReturnStatus);
XNamespace ns = "http://example.com/SystemIntegration";
IEnumerable<XElement> responses = doc.Descendants(ns + "LoginResponse");
foreach (XElement response in responses)
{
    strResponseCode = (string)response.Element(ns + "FirstName");
    strResponseText = (string)response.Element(ns + "LastName");
}

XNamespace Class (System.Xml.Linq)[^]
 
Share this answer
 
C#
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(strReturnStatus);

XmlNamespaceManager xmlnsManager = new System.Xml.XmlNamespaceManager(xmlDoc.NameTable);

xmlnsManager.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
xmlnsManager.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
xmlnsManager.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
xmlnsManager.AddNamespace("si", "http://example.com/SystemIntegration");

// You'd access the full path like this
XmlNode node = xmlDoc.SelectSingleNode("/soap:Envelope/soap:Body/si:LoginResponse/si:FirstName", xmlnsManager);
string firstname = node.InnerText;

// or just like this
node = xmlDoc.SelectSingleNode("//si:FirstName", xmlnsManager);
firstname = node.InnerText;
 
Share this answer
 
Comments
Richard Deeming 4-Feb-16 9:06am    
The LINQ to XML code is cleaner. :)
Luiey Ichigo 14-Mar-19 23:39pm    
simple yet effective
Simple solution, if body has multiple children, and you just wat to remove envelope:
C#
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(placeYourXmlHere);

if (xmlDoc.DocumentElement.Name == "soapenv_Envelope")
{
    string tempXmlString = xmlDoc.DocumentElement.InnerXml;
    xmlDoc.LoadXml(tempXmlString);
}

If you want to remove both Envelope and body, where body only contain one child:
C#
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(placeYourXmlHere);

while (xmlDoc.DocumentElement.Name == "soapenv_Envelope" || xmlDoc.DocumentElement.Name == "soapenv_Body")
{
    string tempXmlString = xmlDoc.DocumentElement.InnerXml;
    xmlDoc.LoadXml(tempXmlString);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900