calling a webservice and deserializing soap without a wsdl - c#

I have a vendor who doesn't seem to have a wsdl or is unwilling or doesn't know how to provide it. They have a number of web services (technically they are JSPs that return soap messages) and I need to use about 10-15 of these to get my stuff done.
Since there isn't a WSDL, I can't use the 'add web reference' functionality to generate proxy classes and such. I've gotten around this by using WebClient to make the calls and return the response as a string, but now I need to deserialize the response into client classes.
I've already made c# classes to match the xml that's returned, but I'm not sure how to deserialize from SOAP since there's so much xml noise. I could strip the SOAP envelope tags and then use the XML serializer to deserialize to a List<SomeType>, but that seems really dirty. Is there a nicer way?

i published an article detailing on how to deserialize a complex xml.
here is the link : http://blog.impact-works.com/2011/06/30/how-to-serializedeserialize-complex-xml-in-asp-net-c/
hope it helps

Have you tried using SoapFormatter.Deserialize() for deserializing the response. If so, this would be helpful.
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.soap.soapformatter.deserialize(v=vs.71).aspx

Related

Calling a Java webservice from C# .NET Core (specifically Workday). How to get xml attributes in the soap request

I am trying to call Workday's web service in .net core c#. The service was written in Java. I'm having problems getting xml attributes to show up in my soap request.
Example: What I want it to look like:
...
<wd:Usage_Data wd:Public="true">
<wd:Type_Data wd:Primary="true">
<wd:Type_Reference>
...
What it does look like:
...
<wd:Usage_Data>
<wd:Type_Data>
<wd:Type_Reference>
...
I cant get the wd:Public="true" to be there in the send request xml output. I am setting it in c#.
What I've done is create a WCF Service Reference to workday, and modified the Reference.cs to https://hr.dovetailsoftware.com/gcox/2014/06/13/getting-started-workday-web-services-using-c/, and tried playing around with the xml serializer a bit, but no luck. Other workday services that don't have any attributes work just fine.
I'm sure its a simple fix but I cant find it. I can post more code if necessary. Thanks
Figured it out. I think it's a Wrokday thing. Posting here in case of anyone else. You actually need to set the "Specified" properties.
phoneType.Usage_Data[0].Public = true;
//not good enough, you need to set this
phoneType.Usage_Data[0].PublicSpecified = true;

How do I remove a specific character from a JSON property?

have a json stringresponse, that I need to deserialize in C#.
The problem is, that one of the json properties is called "contentHash" and can have a value that looks like this: "contentHash": "HY$RF>L#O-;G,2-&F7$TD#EH4\S)M[0L'DOHS\"`H'9<"
This is a problem, since the contentHas value has a " character, which looks like the end of the valuestring, but it is not. I don't need to use the contentHash property, so is there a way to exclude the contentHash property from the deserialization or ignore the " character inside the value of the property?
I imagine you are calling an API in order to get that json payload. I don't know whether you are in control of that API, but what follows holds true in any case.
When you call an API you are basically communicating with a remote resource with which you have agreed some conventions. The fact that the API declares to send you a json response having a certain shape is part of the agreement between you (the API consumer) and the API author. This means that, if the API author has documented that he is able to send you a json payload having a certain shape, then you need to trust him and assume that he will send you that kind of response whenever you call the API.
When you write the code to consume the API remember to send an Accept request header having the value application/json (this is useful when the API is able to serve responses in different formats, for instance json and xml, and supports what is called content negotiation).
When you get a response inspect the Content-Type response header and check whether the response media type actually is application/json. If it isn't, you need to find out a way to handle such an unexpected situation. You can decide to throw an exception for instance, because you are facing an unexpected situation and, probably, there is not a business rule in your application domain to handle it (you were expecting json and you get, for instance, xml. What can you possibly do ? nothing better than throwing an exception probably, because the scenario you are facing is totally unexpected).
Let's suppose you get an application/json response content. Then, you can deserialize it in order to consume it in your application. Don't do that manually. There are several libraries meant to do that; in the .NET space one widely adopted is newtonsoft json. Let the library to deserialize the json you get from the API. In case the string you get is not a valid json, the library will probably throw an exception: that's the right thing to do, again the reasoning is the same illustrated above for the response content type.
If the API returns you an invalid json string, it's not a concern of your own code to fix it, in order to make it valid json. That means that the API author is trying to cheat you, he is not being compliant with the agreement cited above at the beginning of my response. You should let him know that he has a bug, because he is not sending valid json and he should fix it. That's probably a sign that he is not using a proper library to serialize its objects to json.
If you need to validate a string in order to check whether it is valid json, use an online service like json lint.
Long story short: fixing the response content so that it becomes valid json is not a concern of the code meant to consume the API. It's a concern of the API author.

How do I 'decorate' a complex C# class to properly deserialize from a rest endpoint?

When using a rest endpoint, I can test it with RestClient and get well-formed XML back. I can copy and paste the XML into a POCO using VS2012's 'Paste XML as Classes' feature.
Then I try to use Web API to hit the same endpoint and convert the XML into my POCO but usually never works right out of the box.
Usually, for the simpler classes, I can decorate members with a variety of annotations and get it working. Why is this? Why won't the original generated class work with Web API?
[XmlElement("dateAdded")]
[DataMember]
What I am finding is that the more complex XML types returned from the endpoint are nearly impossible to properly deserialize as I don't know how to decorate the class. Where can I find a guide that shows me how to do this?
thanks in advance -
Can you provide a sample snippet of the XML? Without looking at the actual XML, it's hard to tell exactly why it is not working...
One thing to try though is to set the Web API's XmlMediaTypeFormatter to use the XmlSerializer instead of the default DataContractSerializer.
WebApiConfig.cs:
config.Formatters.XmlFormatter.UseXmlSerializer = true;
I have opened up a new question with some details inside. Thanks all for viewing and helping so far!
Using 'Paste XML as Classes' to deserialize a Web API rest response

How to create a web service that receives and sends xml based on xsd files?

I need to create a .NET web service that accepts xml, uses this to query a database, and then returns xml. I have been given xsd files for the request and response. Could someone point me in the right direction for where I start from or an example?
I haven't used WCF before so would prefer to use a simple asmx file to do this. I know how to get the data from the database, so it's the xml and web service bits where I'm lost.
I've tried googling this for a while but don't know where to start. Thanks.
The problem you have is that asmx and WCF are both code-first web service technologies. What this means is that you generally start with classes and the web service stack takes care of exposing your types as XML across the wire.
You are starting with a schema, which is not code. So if you want to use asmx/wcf you need to model your schema in code. You can do this by inferring a class structure from your schema using xsd.exe (or svcutil.exe for WCF).
Alternatively you can model your classes by hand based on the schema definition.
Once you have your classes then you can add declarative attributes to the code (See http://msdn.microsoft.com/en-us/library/83y7df3e.aspx for asmx, DataContract and DataMember for WCF). These attributes control:
how an incoming stream of XML is deserialized to a type when a service request is received, and
how instances of your response types get serialized to XML when passed out of your service
The problem with this approach is that getting your XML to validate against your XSD schemas will be a little bit hit and miss, as you cannot rely 100% on class inference from XSD, and additionally you may miss some fine detail if you are modelling it by hand.
Whichever way you do it you need to make sure that your request and response class instances cleanly serialize into XML which will validate against the XSD schemas you have been given.
Also look at a framework called WSCF-Blue which allows you to do contract-first web service design: http://wscfblue.codeplex.com/
Good luck, if you need any more detail about this please let me know via a comment.
From what I can understand, you need to build a webservice, which will accept XML as input, do some processing and spit out XML.
I assume you have a basic understanding of XML but dont know anything about XSD. In very simple terms, XSD is a document which is used to validate a XML file. Think of it a rule book for how XML file should be structed, you can read more about XSD from W3schools. Dont worry about the XSD to much right now. Get a few sample XML documents, which you need to accept as input and output. Build a console application to parse the sample XML file and get the results from the database. Then use the results to build the output XML by looking at the output sample XML. Once you have that completed, then you can use the .NET classes to validate your input and output XML from the XSD you have.
You can look at this answer to see how validation is done.
Once that is done, you can create your web service to return the XML as string.
Hope this help.

Passing XML document to a web service using WCF

I need to pass an XML document to the web service. Is it possible to simply specify a contract with a method, say
void Method(XmlDocument myDocument);
and implement it in a standard way? I need to keep in my the Silverlight functionality, thus I want to avoid writing the XML to a stream... Is it a good approach?
Thank you in advance for the hints and replies!
Cheers
If i recall correctly, XmlDocument is not serializable (which is required for a webservice).
The easiest way would be to write the xml document to a string ( xml), send this to your webservice and there you can deserialize it again to an XmlDocument
I've personally done this (forced too...). WCF has string buffer and string deserialization limits in the WCF binding which you need to override if you are sending anything of size through the interface.
But that said, if your requirement is to simply send XML then create a class, decorate it with the WCF XML attributes and then declare your interface to send them.
If you are interoperating with an existing service, you should be able to use the WCF service tools in the windows SDK to create a WCF binding against the service for you.
If you must pass in that XML document, pass it as a string. But the whole point of the WCF services is that you don't have to fiddle around with XML yourself - you just call the method and pass it some parameters (ints, strings, your own types) and the WCF runtime handles all the thorny XML stuff for you....

Categories