How can I configure SOAP XML namespaces in web service client request? - c#

I am having trouble calling a 3rd party web-service. I have not received a SOAP fault, but am not getting a valid resultset. A colleague of mine has written a client in RPG on the OS400 and it returns a valid resultset. When comparing the RAW request in Fiddler2 for both requests, the only glaring difference I noticed was that my c# client had SOAP xml elements with xmlns="" and his did not. Is it possible to remove said empty namespace declarations? Please see the referenced SOAP request below:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SendArchitectServiceRequest xmlns="archserver.xsd.dataflux.com">
<serviceName xmlns="">AddressVerify.dmc</serviceName>
<fieldDefinitions xmlns="">
<fieldName>AddressLine_1</fieldName>
<fieldType>STRING</fieldType>
<fieldLength>255</fieldLength>
</fieldDefinitions>
<fieldDefinitions xmlns="">
<fieldName>AddressLine_2</fieldName>
<fieldType>STRING</fieldType>
<fieldLength>255</fieldLength>
</fieldDefinitions>
<fieldDefinitions xmlns="">
<fieldName>City_in</fieldName>
<fieldType>STRING</fieldType>
<fieldLength>255</fieldLength>
</fieldDefinitions>
<fieldDefinitions xmlns="">
<fieldName>State_in</fieldName>
<fieldType>STRING</fieldType>
<fieldLength>255</fieldLength>
</fieldDefinitions>
<fieldDefinitions xmlns="">
<fieldName>Zip</fieldName>
<fieldType>STRING</fieldType>
<fieldLength>255</fieldLength>
</fieldDefinitions>
<fieldDefinitions xmlns="">
<fieldName>Country</fieldName>
<fieldType>STRING</fieldType>
<fieldLength>255</fieldLength>
</fieldDefinitions>
<dataRows xmlns="">
<value>3485 W. Harmon Ave.</value>
<value/>
<value>Las Vegas</value>
<value>NV</value>
<value>89103</value>
<value>United States</value>
<reserved>0</reserved>
</dataRows>
</SendArchitectServiceRequest>
</s:Body>
</s:Envelope>

Your proposed method seems like the easiest approach. One other approach would be to use SOAP extensions to modifiy the SOAP response, removing the empty xmlns attribute. You would modify the SoapClientMessage in the BeforeDeserialize stage of the SoapMessageStage.

Since I have not heard any comments or answers to my second question, I will accept the answer I came across which was altering the auto-generated Reference.cs class XML element declarations from System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified) to System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Qualified).

Related

Connected service does not parse SOAP response correctly

I am working with this SOAP web service in a .NET Framework project. I inspect the XML content of requests and responses, and everything looks good. But the SOAP client (the one Visual Studio creates via Connected Services thing) does not map the response correctly to the entity class.
Here is the request XML.
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">OrderList</Action>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<OrderListRequest xmlns="http://www.n11.com/ws/schemas">
<auth xmlns="">
<appKey>**redacted**</appKey>
<appSecret>**redacted**</appSecret>
</auth>
<searchData xmlns="">
<productId xsi:nil="true" />
<status xsi:nil="true" />
<orderNumber>**redacted**</orderNumber>
<productSellerCode xsi:nil="true" />
<sameDayDelivery xsi:nil="true" />
<sortForUpdateDate>false</sortForUpdateDate>
</searchData>
<pagingData xsi:nil="true" xmlns="" />
</OrderListRequest>
</s:Body>
</s:Envelope>
And here is the response.
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header />
<env:Body>
<ns3:OrderListResponse xmlns="" xmlns:ns3="http://www.n11.com/ws/schemas">
<result>
<status>success</status>
</result>
<pagingData>
<currentPage>0</currentPage>
<pageSize>100</pageSize>
<totalCount>1</totalCount>
<pageCount>1</pageCount>
</pagingData>
<orderList>
<order>
<citizenshipId>**redacted**</citizenshipId>
<createDate>02/10/2020 14:46</createDate>
<id>**redacted**</id>
<orderNumber>**redacted**</orderNumber>
<paymentType>1</paymentType>
<status>2</status>
</order>
</orderList>
</ns3:OrderListResponse>
</env:Body>
</env:Envelope>
As it is visible in the response XML, I get all the information I want perfectly. But as you can see in the screenshot below, all I'm getting in the response object is the citizenshipId, and everything else is either null or the default value for its type.
What could be the problem here? I'm a newbie in .NET so I don't even know how to Google this. I tried a few searches but nothing yielded any related results.
The WSDL is publicly available at https://api.n11.com/ws/OrderService.wsdl.

Removing Attribute value based on value from an XML using VB.Net

I have an XML as below
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope
xmlns="http://com/uhg/uht/uhtSoapMsg_V1"
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header>
<uhtHeader
xmlns="http://com/uhg/uht/uhtHeader_V1">
<consumer>COMET</consumer>
<auditId></auditId>
<sendTimestamp>2020-09-03T18:15:40.942-05:00</sendTimestamp>
<environment>P</environment>
<businessService version="24">getClaimHistory</businessService>
<status>success</status>
</uhtHeader>
</env:Header>
<env:Body>
<srvcRspn
xmlns="http://com/uhg/uht/getClaimHistory_V24">
<srvcErrList arrayType="srvcErrOccur[1]" type="Array">
<srvcErrOccur>
<orig>Foundation</orig>
<rtnCd>00</rtnCd>
<explCd>000</explCd>
<desc></desc>
</srvcErrOccur>
</SrvcErrList>
</srvcRspn>
</env:Body>
</env:Envelope>
I want to remove all the attribute values with "http" like below:
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope
xmlns=""
xmlns:env="">
<env:Header>
<uhtHeader
xmlns="">
<consumer>COMET</consumer>
<auditId></auditId>
<sendTimestamp>2020-09-03T18:15:40.942-05:00</sendTimestamp>
<environment>P</environment>
<businessService version="24">getClaimHistory</businessService>
<status>success</status>
</uhtHeader>
</env:Header>
<env:Body>
<srvcRspn
xmlns="">
<srvcErrList arrayType="srvcErrOccur[1]" type="Array">
<srvcErrOccur>
<orig>Foundation</orig>
<rtnCd>00</rtnCd>
<explCd>000</explCd>
<desc></desc>
</srvcErrOccur>
</SrvcErrList>
</srvcRspn>
</env:Body>
</env:Envelope>
I have tried several ways but none of them has worked for me. Can anyone suggest what is fastest way to do it in VB.NET/C#.
The actual response is very large (approx 100000 lines of XML minimum) and using for each will consume a good amount of time. Is there any parsing method or LINQ query method which can do it faster.
I got the way to do it using Regex as below:
Return Regex.Replace(xmlDoc, "((?<=<|<\/)|(?<= ))[A-Za-z0-9]+:| xmlns(:[A-Za-z0-9]+)?="".*?""", "")
It serves my purpose completely. Thanks Cleptus for your quick reference.

SOAP Service call - Getting error Illegal Request format for element

We are integrating a service from a third party. They have recently upgraded their service, and now with the new wsdl, I keep getting "Illegal Request format for Element".
From my investigations the problem seems to be with the xmlns that is added on the main element. If I use SOAPUI and remove the xmlns from the main element, it works, however visual studio adds it automatically in accordance with whats defined in the wsdl.
What is interesting is that with their previous wsdl, the service works with the xmlns included, it is only with the new wsdl that it throws an exception.
In terms of the wsdl, all I know is that they used JD 12 and manually created the wsdl, however upon comparing it, it looks similar to the old one with the name in the xmlns being the only difference.
This is the request visual studio creates:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<extractacccategElement xmlns="http://gna160ws/Management.wsdl/types/">
<xSecurity>
<timekey></timekey>
<authkey></authkey>
<publkey></publkey>
<version>1.x</version>
</xSecurity>
<xRequest1>
<supplierno></supplierno>
</xRequest1>
</extractacccategElement>
</s:Body>
</s:Envelope>
With the "xmlns="http://gna160ws/Management.wsdl/types/"" causing the problem.
Response:
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Header/>
<env:Body>
<srvc:extractacccategElementResponse xmlns="http://gna160ws/Management" xmlns:srvc="http://gna160ws/Management">
<srvc:result>
<response1Out>
<origin>gna160.extractacccategElement</origin>
<invsql>1200</invsql>
<message>Illegal Request format for extractacccategElement.</message>
</response1Out>
</srvc:result>
</srvc:extractacccategElementResponse>
</env:Body>
</env:Envelope>
When submitting the same request, but without the xmlns, I get a valid response. Example:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<extractacccategElement>
<xSecurity>
<timekey></timekey>
<authkey></authkey>
<publkey></publkey>
<version>1.x</version>
</xSecurity>
<xRequest1>
<supplierno></supplierno>
</xRequest1>
</extractacccategElement>
</s:Body>
</s:Envelope>
Response:
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Body>
<ns0:extractacccategElementResponse xmlns="http://gna160ws/Management" xmlns:srvc="http://gna160ws/Management">
<ns0:result>
<ns0:acccategOut>
<ns0:invsql>0</ns0:invsql>
<ns0:message>Success</ns0:message>
<ns0:origin>gna160pkg.ExtractAccCateg</ns0:origin>
<ns0:stage>1</ns0:stage>
<ns0:acccategcode>A</ns0:acccategcode>
<ns0:acccategname>AAA</ns0:acccategname>
</ns0:acccategOut>
</ns0:result>
</ns0:extractacccategElementResponse>
</env:Body>
</env:Envelope>
Additionally, if I add a qualifer after the xmlns then it also works??
Example:
xmlns:hello="http://gna160ws/Management.wsdl/types/"
I have been working collaboratively with the developer on their side and we have not yet been able to identify the problem.
If anyone could help or point me in the right direction, it would be greatly appreciated.
Did you update your service reference with the new WSDL?
Alternatively you can try this answer from Rick Strahl: override the following function to add a custom namespace.
protected override void OnWriteStartEnvelope(XmlDictionaryWriter writer)
{
writer.WriteStartElement("soapenv", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/");
writer.WriteAttributeString("xmlns", "oas", null, "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
writer.WriteAttributeString("xmlns", "v2", null, "http://www.royalmailgroup.com/api/ship/V2");
writer.WriteAttributeString("xmlns", "v1", null, "http://www.royalmailgroup.com/integration/core/V1");
writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
writer.WriteAttributeString("xmlns", "xsd", null, "http://www.w3.org/2001/XMLSchema");
}
Link to his complete article

How to create SOAP XML request message for paypal CreateInvoice api using c#

I have created a sandbox test accounts for PayPal.
I want to consume the PayPal api CreateInvoice,SendInvoice and CreateAndSendInvoice using SOAP xml format in C#. The documentation from x.com, doesn't show any completed request message at least in basic soap xml format, instead, it shows only the header part and definitions for soap xml tags.
Some examples are in JSON format but its not my preferred format,its light-weight but human readable. SDK's are using NVP format, although they have SOAP option but the codes are not able to compose the soap xml format for the payload.
I need the completed soap xml request message with at least the required fields to createinvoice.
I'm still searching stackoverflow so far.
The API reference provides a graphical representation of the SOAP request. For example, you can take a look at CreateAndSendInvoice and see all of the tags that could be included in your XML/SOAP request as well as how everything should be nested.
If you're going to be building the XML yourself as oppose to using a WSDL there's really no need to format it for SOAP. Here's a sample of an XML Request for CreateInvoice that I just ran successfully...
<?xml version="1.0" encoding="utf-8"?>
<CreateInvoiceRequest xmlns="http://svcs.paypal.com/types/ap">
<requestEnvelope xmlns="">
<detailLevel>ReturnAll</detailLevel>
<errorLanguage>en_US</errorLanguage>
</requestEnvelope>
<invoice xmlns="">
<merchantEmail xmlns="">sandbo_1215254764_biz#angelleye.com</merchantEmail>
<payerEmail xmlns="">sandbo_1204199080_biz#angelleye.com</payerEmail>
<number xmlns="">12Z3-ABCDE</number>
<merchantInfo xmlns="">
<firstName xmlns="">Tester</firstName>
<lastName xmlns="">Testerson</lastName>
<businessName xmlns="">Testers, LLC</businessName>
<phone xmlns="">555-555-5555</phone>
<fax xmlns="">555-555-5556</fax>
<website xmlns="">http://www.domain.com</website>
<customValue xmlns="">Some custom info.</customValue>
<address xmlns="">
<line1 xmlns="">123 Main St.</line1>
<city xmlns="">Grandview</city>
<state xmlns="">MO</state>
<postalCode xmlns="">64030</postalCode>
<countryCode xmlns="">US</countryCode>
</address>
</merchantInfo>
<itemList xmlns=""><item xmlns="">
<name xmlns="">Test Widget 1</name>
<description xmlns="">This is a test widget #1</description>
<date xmlns="">2012-02-18</date>
<quantity xmlns="">1</quantity>
<unitPrice xmlns="">10.00</unitPrice>
</item><item xmlns="">
<name xmlns="">Test Widget 2</name>
<description xmlns="">This is a test widget #2</description>
<date xmlns="">2012-02-18</date>
<quantity xmlns="">2</quantity>
<unitPrice xmlns="">20.00</unitPrice>
</item></itemList>
<currencyCode xmlns="">USD</currencyCode>
<paymentTerms xmlns="">DueOnReceipt</paymentTerms>
<note xmlns="">This is a test invoice.</note>
<merchantMemo xmlns="">This is a test invoice.</merchantMemo>
<billingInfo xmlns="">
<firstName xmlns="">Tester</firstName>
<lastName xmlns="">Testerson</lastName>
<businessName xmlns="">Testers, LLC</businessName>
<phone xmlns="">555-555-5555</phone>
<fax xmlns="">555-555-5556</fax>
<website xmlns="">http://www.domain.com</website>
<customValue xmlns="">Some custom info.</customValue>
<address xmlns="">
<line1 xmlns="">123 Main St.</line1>
<city xmlns="">Grandview</city>
<state xmlns="">MO</state>
<postalCode xmlns="">64030</postalCode>
<countryCode xmlns="">US</countryCode>
</address>
</billingInfo>
<shippingInfo xmlns="">
<firstName xmlns="">Tester</firstName>
<lastName xmlns="">Testerson</lastName>
<businessName xmlns="">Testers, LLC</businessName>
<phone xmlns="">555-555-5555</phone>
<fax xmlns="">555-555-5556</fax>
<website xmlns="">http://www.domain.com</website>
<customValue xmlns="">Some custom info.</customValue>
<address xmlns="">
<line1 xmlns="">123 Main St.</line1>
<city xmlns="">Grandview</city>
<state xmlns="">MO</state>
<postalCode xmlns="">64030</postalCode>
<countryCode xmlns="">US</countryCode>
</address>
</shippingInfo>
<shippingAmount xmlns="">10.00</shippingAmount>
<logoUrl xmlns="">https://www.usbswiper.com/images/angelley-clients/cpp-header-image.jpg</logoUrl>
<referrerCode xmlns="">AngellEYE_PHPClass</referrerCode>
</invoice>
</CreateInvoiceRequest>

Convert XML namespace prefixes with C#?

I have run into an exasperating problem getting a Java service client to communicate successfully with a WCF service. I have overcome many hurdles, and I believe that this is my last one. The problem boils down to how Java Axis + WSS4J seem to handle xml namespaces. The Java platform seem to be very rigid in what they expect for xml namespace prefixes, and as such, do not understand the WCF reply messages.
My problem in a nutshell is as follows. I have an xml response similar to the following from my WCF service:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<s:Header>
<a:Action s:mustUnderstand="1" u:Id="_3">http://tempuri.org/IProcessor/DoProcessingResponse</a:Action>
<h:CorrelationID xmlns:h="http://tempuri.org/">1234</h:CorrelationID>
<a:RelatesTo u:Id="_4">uuid:40f800a0-9613-4f4a-96c5-b9fd98085deb</a:RelatesTo>
<o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<!-- WS-Security header stuff -->
</o:Security>
</s:Header>
<s:Body u:Id="_1">
<e:EncryptedData Id="_2" Type="http://www.w3.org/2001/04/xmlenc#Content" xmlns:e="http://www.w3.org/2001/04/xmlenc#">
<e:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc"/>
<e:CipherData>
<e:CipherValue>NfA6XunmyLlT2ucA+5QneoawHm+imcaCltDAJC1mRZOSxoB6YGpDLY1FyVykPbPGDoFGUESLsmvvbD62sNnRrgE+AuKPo+1CD3DF4LfurRcEv9A50ba9V+ViqlrhydhK</e:CipherValue>
</e:CipherData>
</e:EncryptedData>
</s:Body>
</s:Envelope>
This response uses simple one-character namespace prefixes for most things, such as 's' for SOAP Envelope, 'a' for WS-Addressing, 'o' for 'WS-Security', etc. The Java client, namely WSS4J, seems to expect the following:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<soap:Header>
<wsa:Action soap:mustUnderstand="1" wsu:Id="_3">http://tempuri.org/IProcessor/DoProcessingResponse</wsa:Action>
<h:CorrelationID xmlns:h="http://tempuri.org/">1234</h:CorrelationID>
<wsa:RelatesTo wsu:Id="_4">uuid:40f800a0-9613-4f4a-96c5-b9fd98085deb</a:RelatesTo>
<wsse:Security soap:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<!-- WS-Security header stuff -->
</wsse:Security>
</soap:Header>
<soap:Body u:Id="_1">
<xenc:EncryptedData Id="_2" Type="http://www.w3.org/2001/04/xmlenc#Content" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
<xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc"/>
<xenc:CipherData>
<xenc:CipherValue>NfA6XunmyLlT2ucA+5QneoawHm+imcaCltDAJC1mRZOSxoB6YGpDLY1FyVykPbPGDoFGUESLsmvvbD62sNnRrgE+AuKPo+1CD3DF4LfurRcEv9A50ba9V+ViqlrhydhK</xenc:CipherValue>
</xenc:CipherData>
</xenc:EncryptedData>
</soap:Body>
</soap:Envelope>
Upon receipt of my response message, the Java client and WSS4J seem to want to look up elements by their own internal xml aliases, such as 'wsa' for WS-Addressing, and 'wsse' for WS-Security Extensions. Since neither of those namespaces are present in the actual response xml, exceptions are thrown.
I am wondering if there is any simple way to transform an xml document from one set of namespaces to another set using C#, .NET, and the System.Xml namespace. I've poked around with XmlNamespaceManager a bit, but it does not seem to fully support what I need...or at least, I have been unable to find any really useful examples, and am not fully sure how it works. I am trying to avoid having to write some heavy-duty process to handle this manually myself, as I do not want to drastically impact the performance of our services when called by a Java Axis/WSS4J client.
This particular problem seemed to stem from an old and buggy version of WSS4J. Newer versions do not seem to have this problem, and it is no longer an issue.

Categories