How to read soap response in c#? - c#

Soap Respose as:
<?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:
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#??

The easiest thing is to create a proxy class for your service.
You can do that using the 'Add Service Reference' option in Visual Studio. Enter the URL of the service, and Visual Studio will generate the source code for you.
From that point you can access the service using C# code. No need to manually extract the payload of the SOAP message.
Note if you are the implementator of the service: ASMX webservices are deprecated for a long time already. If you can, use WCF.

Use Descendants() method in XDocumentobject to navigate through XML nodes to get elements, you can follow steps provided in this post Using C# to parse a SOAP Response

Related

Adding custom SOAPHeader in C# for a web service call

I am trying to add a custom soap header information in c# before calling a web service. I am using SOAP Header class to get this done. I could do this partly but not completely the way I need it. Here is how I need the soap header to look like
<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:Header>
<Security xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<UsernameToken>
<Username>USERID</Username>
<Password>PASSWORD</Password>
</UsernameToken>
</Security>
</soap:Header>
<soap:Body>
...
I am able to add soap header as below
<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:Header>
<UsernameToken xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<Username>UserID</Username>
<Password>Test</Password>
</UsernameToken>
</soap:Header>
<soap:Body>
What I am not able to do is add the "Security" elements which wraps the "UsernameToken" as in the first sample. Any help would be appreciated.
This link adding soap header worked for me. I am calling a SOAP 1.1 service that I did not write and have no control over. Am using VS 2012 and added the service as a web reference in my project. Hope this helps
I followed the steps 1-5 from J. Dudgeon's post towards the bottom of the thread.
Here's some sample code (this would be in a separate .cs file):
namespace SAME_NAMESPACE_AS_PROXY_CLASS
{
// This is needed since the web service must have the username and pwd passed in a custom SOAP header, apparently
public partial class MyService : System.Web.Services.Protocols.SoapHttpClientProtocol
{
public Creds credHeader; // will hold the creds that are passed in the SOAP Header
}
[XmlRoot(Namespace = "http://cnn.com/xy")] // your service's namespace goes in quotes
public class Creds : SoapHeader
{
public string Username;
public string Password;
}
}
And then in the generated proxy class, on the method that calls the service, per J Dudgeon's step 4 add this attribute: [SoapHeader("credHeader", Direction = SoapHeaderDirection.In)]
finally, here's the call to the generated proxy method, with the header:
using (MyService client = new MyService())
{
client.credHeader = new Creds();
client.credHeader.Username = "username";
client.credHeader.Password = "pwd";
rResponse = client.MyProxyMethodHere();
}

capture xml from external web service parse through nodes

I have a c# web app that has a simple task: consume an external web service, and loop through its returned xml.
I am consuming the web service like this:
WSExternal.SoapHeaderCredentials objHeaderCredentials = new WSExternal.SoapHeaderCredentials();
objHeaderCredentials.UserName = "username";
objHeaderCredentials.Password = "password";
WSExternal.MembershipAuthenticationService objClient = new WSExternal.MembershipAuthenticationService();
objClient.SoapHeaderCredentialsValue = objHeaderCredentials;
I can call the single exposed web method like this:
objClient.GetMembershipUserList(false);
If I hit the web service through a client like SoapUI or XMLSpy, it returns xml like this:
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetMembershipUserListResponse xmlns="http://namespaceofwebservice">
<GetMembershipUserListResult>
<UserList>
<MembershipUserData>
<id>1</id>
<UserName>userone</UserName>
<PrimaryFirstName>User</PrimaryFirstName>
<PrimaryLastName>One</PrimaryLastName>
<Address>13 MAIN STREET</Address>
<City>ROCHESTER</City>
</MembershipUserData>
<MembershipUserData>
<id>2</id>
<UserName>usertwo</UserName>
<PrimaryFirstName>User</PrimaryFirstName>
<PrimaryLastName>Two</PrimaryLastName>
<Address>15 WEST AVE</Address>
<City>JERSEY CITY</City>
</MembershipUserData>
</UserList>
<Page>0</Page>
</GetMembershipUserListResult>
</GetMembershipUserListResponse>
</soap:Body>
My question is, how do I "capture" this xml from the web method, and loop through it to get the relevant data out of the nodes?

SOAP API with DomainBox in ASP.NET application

I am trying to integrate an ASP.NET application to http://www.domainbox.com/ using their SOAP API. I couldn't find any examples. The only thing they offer is the SOAP Resquest and Response examples. My question is how do I call the request and how to I get the response data to use in my application?
REQUEST:
<soap12:Envelope xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:soap12=”http://www.w3.org/2003/05/
soap-envelope”>
<soap12:Body>
<QueryDomainRenewalSettings xmlns=”https://sandbox.domainbox.net/”>
<AuthenticationParameters>
<Reseller>myreseller</Reseller>
<Username>myusername</Username>
<Passwordmy>password</Password>
</AuthenticationParameters>
<CommandParameters>
<DomainName>atestdomain.co</DomainName>
</CommandParameters>
</QueryDomainRenewalSettings>
</soap12:Body>
</soap12:Envelope>
RESPONSE:
<soap:Envelope xmlns:soap=”http://www.w3.org/2003/05/soap-envelope”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=”http://www.
w3.org/2001/XMLSchema”>
<soap:Body>
<QueryDomainRenewalSettingsResponse xmlns=”https://sandbox.domainbox.net/”>
<QueryDomainRenewalSettingsResult>
<ResultCode>100</ResultCode>
<ResultMsg>Domain Renewal Settings Queried Successfully</ResultMsg>
<TxID>4d76201a-3b6d-4ccc-bca5-729439bbac9b</TxID>
<DomainId>87967</DomainId>
<AutoRenew>true</AutoRenew>
<AutoRenewDays>60</AutoRenewDays>
</QueryDomainRenewalSettingsResult>
</QueryDomainRenewalSettingsResponse>
</soap:Body>
</soap:Envelope>
Thanks.
You should add the Domainbox API as a service reference if you're using ASP.NET.
That will let you use it just like a normal C#/VB class,
var parameters = new Domainbox.CheckDomainAvailabilityParameters {DomainName= "example.com"};
var result = apiObject.CheckDomainAvailiability(authObject, paramters);
if (result.ResultCode == 100) { // get results... }
I can provide more detailed example if required.

How to retrieve a collection out of an SOAP message

I'm using a webservice for render company information based on a special company number.
But i can not get data out of the response SOAP message.
You can see an example of the response soap message. (I left out some company information the tags).
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Body>
<businessGetDossierV3Response xmlns="http://www.webservices.nl/soap/">
<out>
<paging>
<curpage>1</curpage>
<perpage>10</perpage>
<numpages>1</numpages>
<numresults>1</numresults>
<maxresults>100</maxresults>
</paging>
<results>
<item>
<DossierNo></DossierNo>
<SubDossierNo></SubDossierNo>
<ChamberNo></ChamberNo>
<Legalformcode></Legalformcode>
<LegalformcodeText></LegalformcodeText>
<Tradename45></Tradename45>
<EstablishmentPostcode></EstablishmentPostcode>
<EstablishmentCity></EstablishmentCity>
<EstablishmentStreetname></EstablishmentStreetname>
<EstablishmentHouseNo></EstablishmentHouseNo>
<CorrespondencePostcode></CorrespondencePostcode>
<CorrespondenceCity></CorrespondenceCity>
<CorrespondenceStreetname></CorrespondenceStreetname>
<CorrespondenceHouseNo></CorrespondenceHouseNo>
</item>
</results>
</out>
</businessGetDossierV3Response>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
! this is the result i see on the demo client !
But this is my code in C#
nl.webservices.ws1.BusinessDossierV3PagedResult result = null;
result = myserviceBusiness.businessGetDossierV3(KVKnr, "0000", 1);
I want to do something like this (but with the results instead of paging part)
int page = result.paging.numpages;
Then I should be like this I suppose
string city = result.results.item.CorrespondenceCity;
But this is giving a fault message
So in Visual studio 2010, i can only retrieve the data in the paging part of the xml and put it into a textbox, but not from results part. Is this because the result part is some kind of Collection?
So yes, how can i put the data from the tags EstablishmentPostcode and EstablishmentCity in a textbox on my default page?
thanks in advance
You could try adding your SOAP service as a web reference to the project. http://msdn.microsoft.com/en-us/library/tydxdyw9.aspx
If you don't want to do this and would rather work on the XML directly you could use xpath to get access to all the item elements in your results element.
http://www.stardeveloper.com/articles/display.html?article=2009031001&page=1
One thing to be careful of when using xpath is that you use the correct xml namespace for the node you're trying to select.
Ben

Consuming in Java a .NET Web Service that requires a custom SOAP Header

So I need to consume a Web Service that uses a custom SoapHeader, as described below. What is the simplest way to pass the correct values through this header using Java. I'm using Netbeans.
<?xml version="1.0" encoding="utf-8"?\>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<CustomSoapHeader xmlns="http://snip">
<UserName>"string"</UserName>
<Password>"string"</Password>
</CustomSoapHeader>
</soap:Header>
<soap:Body>
<SomeWebMethod xmlns="http://snip" />
</soap:Body>
</soap:Envelope>
EDIT: What's the best way to display XML on Stack Overflow?
It might help to add that the Web Service is implemented in .NET and I cannot change the server side code.
Here are the basic steps, assuming you're doing this on the client side:
Install a HandlerResolver on your service interface (service.setHandlerResolver())
Override HandlerResolver.getHandlerChain() to insert your own implementation of SOAPHandler
Implement SOAPHandler.handleMessage() to modify the SOAP header before it's sent out
You can pass parameters to your handler through the request context:
Map<String, Object> context = ((BindingProvider) port).getRequestContext();
context.put("userName', "foo");
...
in handleMessage() you can get at the header like this:
public boolean handleMessage(SOAPMessageContext context) {
...
SOAPMessage msg = context.getMessage();
msg.getSoapHeader();
...
}
Hope that helps. I'm guessing there's also a way to do this stuff with annotations as well.

Categories