Webservice XML response deserialization
I have the WSDL for a web service that I cannot currently connect to. The web service has a method called ExecuteCommand
which returns a CommandResult
CommandResult looks like:
string ResultMessage;
DataSet ResultDataset;
bool ResultSuccess;
I've added the WebService as a Service Reference, but since I cannot currently reach the actual WebService, I need to work off of an XML file that is (I'm told) the XML Response that the WebService would return should I call it's ExecuteCommand
method.
If the WebService was working, I would simply do something like:
MyWebService.ServiceDataInterfaceSoapClient ws = new ServiceDataInterfaceSoapClient();
MyWebService.CommandResult result = ws.ExecuteCommand();
Instead, I want to do something like:
MyWebService.CommandResult result = //Load Result from XML file which contains the XML response that ws.ExecuteCommand would return.
开发者_Go百科I'm not sure how to go about this. I attempted to create a XmlSerializer
of type CommandResult
, but I get XML parsing errors on the very first line. I'm hoping someone can point me to a basic example on how to do this.
I ended up figuring out the following and it's doing what I needed:
XmlSerializer xml = new XmlSerializer(typeof(CommandResult),"XMLNamespaceFromWSDL");
CommandResult cr;
using (Stream stream = new FileStream("CommandResult.xml", FileMode.Open))
cr = (CommandResult)xml.Deserialize(stream);
Sounds like you're attempting to recreate what WCF normally does for you. It's probably simpler to create a new local WCF service that implements ServiceContract interface SvcUtil creates for you. In the local service code, you would just new-up a MyWebService.CommandResult object and feed it the correct values from the provided file. That way the client code would not need to be modified to consume the XML response stored in the file. This approach would work even if you are trying to minic a non-WCF based service. Just feed SvcUtil the WSDL and use a basicHttpBinding to mock any Basic Profile (asmx & some java based) service. More detail about how the ServiceDataInterfaceSoapClient object is created would be helpful.
精彩评论