Here is my current situation. I had to break standards for how to do web services calls. We had 60+ WSDLs to connect to and the name changed based on a parameter. I created the web service call dynamically using a WebClient and built the envelope manually (it was decided to do this instead of adding each web service by upper management). I get a string response back that has the valid XML that matches the XSD. I’m trying to figure out how to create a class to reference the fields dynamically instead of a class per WSDL (build a class based on the XSD being used). Here is what I’m doing:
public string results;
using (WebClient client = new WebClient())
{
string soapENV = @"http://schemas.xmlsoap.org/soap/envelope/";
var payload = @"<?xml version=""1.0"" encoding=""utf-8""?>" +
"<SOAP-ENV:Envelope xmlns:SOAP-ENV='" + soapENV + "'>" +
"<SOAP-ENV:Header/>" +
"<SOAP-ENV:Body>" +
"<" + myParams.requestName + " xmlns='" +
myParams.requestNamespace + "'>" +
"<MtvnSvcVer>1.0</MtvnSvcVer>" +
"<MsgUUID>" + UUID + "</MsgUUID>" +
"<PrcsParms>" +
"<SrcID>" + currentVendorID + "</SrcID>" +
"</PrcsParms>"
................
"</SOAP-ENV:Body>" +
"</SOAP-ENV:Envelope>";
this.results = client.UploadString (URL, payload);
}
For the sake of saving time and space I took out some of the XML tags. The XSD response has the same values for XML tags as above. I then call this to be executed as follows:
string results = connectwareService.InvokeConnectWare();
The results returned look like this:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<DPNmeAddrInqMtvnSvcRes xmlns="mtvnCWDPNmeAddrInqSvcRes">
<MtvnSvcVer>1.0</MtvnSvcVer>
<MsgUUID>DATA HERE</MsgUUID>
<Svc>
<SvcParms>
<ApplID>DATA HERE</ApplID>
<SvcID>DATA HERE</SvcID>
<SvcVer>1.0</SvcVer>
<RqstUUID>DATA HERE</RqstUUID>
</SvcParms>
<MsgData>
<DPNmeAddrInqResData xmlns="mtvnCWDPNmeAddrInqResData">
<E20007>DATA HERE</E20007>
........
</soapenv:Body>
</soapenv:Envelope>
Is there a way in the code to do this? I keep seeing peopele refer to using xsd.exe. Can that be executed in code behind? I was thinking that I could read through and populate a generic class with the tag name as a name and the data as a value as last resort but there has to be a way to generate this on the fly in the code. I guess another way I could do it is read all 60+ wsdls and run xsd.exe to build the classes needed?
Also, Just some FYI – I’m converting old Cold Fusion code to C# and I’m trying to figure out how to replace its “ConvertXmlToStruct” method. Any help would be greatly appreciated.
xsd.exe is only run once to build a class file that represents the objects defined in the XSD. You then use an XmlSerializer class to actually convert (aka deserialize) the XML to the c# class.
There are a lot of links out there talking about deserializing XML in C#:
How to use xsd in c#?
http://msdn.microsoft.com/en-us/library/ms950721.aspx