Today we'll take a look at how to use Services Test's 'Custom Code' activity to save some elements returned by a web service's response to a XML file.
- Start the Sample Application that comes with Service Test (Start\Programs\Hp Service Test 11.10\Sample Application.
-
Write down the WSDL's endpoint: http://localhost:24240/HPFlights_SOAP?WSDL
- Create a new test in ST and Click on the Import Service

- In the Import Service from URL or UDDI's Address field enter: http://localhost:24240/HPFlights_SOAP?WSDL
-
Under ST's 'Toolbox' under the 'Web Services' folder you should have a GetFlights operation. Drag that to the main canvas area.

-
In the GetFlights Property Sheet enter the following:
DepartureCity= Denver
ArrivalCity = London - Under the Toolbox Miscellaneous folder drag a custom code onto the canvas
-
Under the Custom code's property sheet click on 'Add Input Property'

- Name the input property flightXML type 'String'
-
Link the flightXML value to the GetFlights output GetFlightResponse

- Click on the Custom Codes property sheet 'Events'(thunderbolt) icon
- Select 'Create a default handler' under the 'ExecuteEvent'
- Save your ST test up to this point
- In your custom code CodeActivity_OnExecuteEvent enter the following:
XmlDocument xml1 = new XmlDocument();
xml1.LoadXml(this.CodeActivity5.Input.flightXML);
//Get the number of flight returned
XmlNodeList fNList = xml1.GetElementsByTagName("FlightNumber");
int fCount = fNList.Count;
// Create the xml document to hold flight info
XmlDocument doc = new XmlDocument();
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", null, null);
doc.AppendChild(dec); // Create the root element
XmlElement root = doc.CreateElement("GetFlights");
doc.AppendChild(root);
// Create a Flight retunred count element
XmlElement FlightCount = doc.CreateElement("FlightCount");
FlightCount.InnerText = fCount.ToString();
root.AppendChild(FlightCount);
for(int i=0;i)
{
XmlElement Flights = doc.CreateElement("Flights");
//Create a flight element that will hold the flight num
XmlElement flight = doc.CreateElement("flight");
/Assign the retunred flight num to element
flight.InnerText = fNList[i].InnerText;
Flights.AppendChild(flight);
root.AppendChild(Flights);
}
string xmlOutput = doc.OuterXml;
//Write XML to file
doc.Save("D:\\Flights.XML");
15. Run the test. The test should run without error and you should now have an XML file that looks like the following:

Next time we'll take a look at how to read this XML and data drive the CreateFlights operation.

{ 5 comments… read them below or add one }
Hi Joe, i am getting at error at the line “for(int i=0;i)” . I tried to change the loop to for (int i=0;i<100;i++). Now i am getting custom code functionality error saying that "Object reference not set to an instance of an object.". Can you please help me out on this?
I am getting same error,could you please guide us.
I am getting an error “Data at the root level is invalid. Line 1, position 1.”.
At root level I have
namespace Script
{
using System;
using System.Xml;
using System.Xml.Schema;
using HP.ST.Ext.BasicActivities;
using HP.ST.Fwk.RunTimeFWK;
using HP.ST.Fwk.RunTimeFWK.ActivityFWK;
using HP.ST.Fwk.RunTimeFWK.Utilities;
using HP.ST.Fwk.RunTimeFWK.CompositeActivities;
using System.Windows.Forms;
Any idea about the possible reason behind this?Thanks in Advance!
abc » I don’t think I ever asked you – what version of Service Test are you using?
Hi Joe,
I am also getting the same error, “Object reference not set to an instance of an object”
I am using the version 11.20.