Write WCF output to document
-
28-06-2021 - |
Question
Hi I have a WCF service which is working good. for testing purpose to QC the data i would like to seriliaze the data and write it to an xml document. how can this be done.
please find the below code where im consuming the WCF service in a client app
Client.EMPServiceClient proxy = new Client.EMPServiceClient();
proxy.ClientCredentials.UserName.UserName = "testuser";
proxy.ClientCredentials.UserName.Password = "password";
Client.EMPSearchCriteria criteria = new Client.EMPSearchCriteria();
criteria.EMPNumber = "01-351";
proxy.GetEMPData(criteria);
Console.Write("Finish");
I wrote a class as below to write the output to a doc - but could some one tell me how to bridge these
public static void SerializeToXML(EMPData pdata)
{
XmlSerializer serializer = new XmlSerializer(typeof(EMPData));
TextWriter txtwriter = new StreamWriter(@"d:\test.xml");
serializer.Serialize(txtwriter, pdata);
txtwriter.Close();
}
Please advice on how to write the output to an xml doc
Thanks, Justin
Solution
Doesn't
proxy.GetEMPData(criteria);
return something? Shouldn't you use that result?
Try...
Client.EMPServiceClient proxy = new Client.EMPServiceClient();
proxy.ClientCredentials.UserName.UserName = "testuser";
proxy.ClientCredentials.UserName.Password = "password";
Client.EMPSearchCriteria criteria = new Client.EMPSearchCriteria();
criteria.EMPNumber = "01-351";
var data = proxy.GetEMPData(criteria); // Change this line
SerializeToXML(data); // and adding this line
Console.Write("Finish");
OTHER TIPS
The right way to do this is with WCF's built-in message logging- no need to modify the app at all. This way, you're sure to get exactly the same message- otherwise your client's serialization can be affected by WCF configuration that won't apply when you serialize the message manually.