Created On:  02 November 2011

Problem:

How do I export a TestPlan via the Services Exchange in C#?

Resolution:

The System.Net namespace provides a number of classes for interacting and communicating with Web Applications.

Using the classes available in this namespace we can send a request to the Services Exchange to export a Testplan node and read the response natively within C#. This response, which is XML based, can then be written to a document for modification or use in the fashion desired. 

The code below demonstrates how we can export a Testplan Node to XML:


        public static void exportNode(String sServer, int iPort, String sUser, String sPassword, int iNode)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("
http://"+sServer+":"+iPort+"/servicesExchange?hid=exportTestPlan&userName="+sUser+"&passWord="+sPassword+"&nodeID="+iNode+"");
            request.Credentials = CredentialCache.DefaultCredentials;

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream recStream = response.GetResponseStream();
            StreamReader readStream = new StreamReader(recStream, Encoding.UTF8);

            System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\tp.xml");
            file.WriteLine(readStream.ReadToEnd());
            file.Close();
            readStream.Close();
            recStream.Close();
        }