I have a web service called "Add".
It simply takes 2 numbers, sums them, and gives a result. (example 1+1)
The result is loaded in a stand-alone page with a URL "http://localhost:xxxx/MathService.asmx/Add
And the result is given by xML :
xml version="1.0" encoding="utf-8" ?>
<int xmlns="http://tempuri.org/">2int>
My Question is "how do I parse the result ,2, within the XML"?
I am a newbie at C#
~Jae
10 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Kirtan PatelPosted Feb 12, 2010, 4:59 PM
you can Call method By simply Creating Intance of WebService and call the method like below
j lPosted Feb 15, 2010, 1:38 PM
Things to know:
The web service ADDS 2 values ("a" and "b") from an Excel spreadsheet called Math.csv (included in zip)
The ADD Result is always given on a new page as an integer at the URL http://localhost:xxxx/MathService.asmx/Add
And the URL result page will include the answer and will look like this :
xml version="1.0" encoding="utf-8" ?>
I want to always be able to parse the result...in this case "2"
Kirtan PatelPosted Feb 15, 2010, 11:47 AM
j lPosted Feb 15, 2010, 11:36 AM
This web service always Adds two numbers, so the code needs to be dynamic. How do I make the code dynamically read ALL data?
The Add answer page is a URL, http://localhost:xxxx/MathService.asmx/Add, that generates the answer XML...in this case the answer was "2" (example 1+1)
xml version="1.0" encoding="utf-8" ?>
Kirtan PatelPosted Feb 15, 2010, 11:12 AM
you have to take
string XMLDataGotFromWebService ="xml version="1.0" encoding="utf-8" ?> <int xmlns="http://tempuri.org/">2int>";
Whole String not first Line only to work Code ..
Thanks :) thats why its not working ..
j lPosted Feb 15, 2010, 9:00 AM
at this line "doc.load("temp.xml");
Do you know why? My code is below
thanks
j
//------------------------------------------------------------------------------
//
// This code was generated by a tool.
// Runtime Version:4.0.21006.1
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//
//------------------------------------------------------------------------------
namespace TestProject3
{
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TestTools.WebTesting;
using Microsoft.VisualStudio.TestTools.WebTesting.Rules;
using System.Xml.Linq;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Xml;
[DeploymentItem("testproject3\\MathData.csv", "testproject3")]
[DataSource("mathdata", "Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\testproject3\\MathData.csv", Microsoft.VisualStudio.TestTools.WebTesting.DataBindingAccessMethod.Sequential, Microsoft.VisualStudio.TestTools.WebTesting.DataBindingSelectColumns.SelectOnlyBoundColumns, "MathData#csv")]
[DataBinding("mathdata", "MathData#csv", "a", "mathdata.MathData#csv.a")]
[DataBinding("mathdata", "MathData#csv", "b", "mathdata.MathData#csv.b")]
[DataBinding("mathdata", "MathData#csv", "Result", "mathdata.MathData#csv.Result")]
public class mathTest1Coded : WebTest
{
public mathTest1Coded()
{
this.PreAuthenticate = true;
}
public override IEnumerator<WebTestRequest> GetRequestEnumerator()
{
// Initialize validation rules that apply to all requests in the WebTest
if ((this.Context.ValidationLevel >= Microsoft.VisualStudio.TestTools.WebTesting.ValidationLevel.Low))
{
ValidateResponseUrl validationRule1 = new ValidateResponseUrl();
this.ValidateResponse += new EventHandler<ValidationEventArgs>(validationRule1.Validate);
}
if ((this.Context.ValidationLevel >= Microsoft.VisualStudio.TestTools.WebTesting.ValidationLevel.Low))
{
ValidationRuleResponseTimeGoal validationRule2 = new ValidationRuleResponseTimeGoal();
validationRule2.Tolerance = 0D;
this.ValidateResponseOnPageComplete += new EventHandler<ValidationEventArgs>(validationRule2.Validate);
}
WebTestRequest request1 = new WebTestRequest("http://localhost:1752/MathService.asmx");
request1.ThinkTime = 4;
yield return request1;
request1 = null;
WebTestRequest request2 = new WebTestRequest("http://localhost:1752/MathService.asmx");
request2.ThinkTime = 5;
request2.QueryStringParameters.Add("op", "Add", false, false);
yield return request2;
request2 = null;
WebTestRequest request3 = new WebTestRequest("http://localhost:1752/MathService.asmx/Add");
request3.Method = "POST";
FormPostHttpBody request3Body = new FormPostHttpBody();
request3Body.FormPostParameters.Add("a", this.Context["mathdata.MathData#csv.a"].ToString());
request3Body.FormPostParameters.Add("b", this.Context["mathdata.MathData#csv.b"].ToString());
request3Body.FormPostParameters.Add("Parameter1", this.Context["mathdata.MathData#csv.Result"].ToString());
request3Body.FormPostParameters.Add("MathAns", "");
request3.Body = request3Body;
if ((this.Context.ValidationLevel >= Microsoft.VisualStudio.TestTools.WebTesting.ValidationLevel.High))
{
ValidationRuleFindText validationRule3 = new ValidationRuleFindText();
validationRule3.FindText = (this.Context["mathdata.MathData#csv.Result"].ToString());
validationRule3.IgnoreCase = false;
validationRule3.UseRegularExpression = false;
validationRule3.PassIfTextFound = true;
request3.ValidateResponse += new EventHandler<ValidationEventArgs>(validationRule3.Validate);
}
int result;
string XMLDataGotFromWebService = "+'"'+"1.0"+'"'+" encoding="+'"'+"utf-8"+'"'+" ?>";
//Write data in XML File for Parsing
File.WriteAllText("temp.xml", XMLDataGotFromWebService);
//Load the File and Parse the Needed thing
XmlDocument doc = new XmlDocument();
doc.Load("temp.xml");
foreach (XmlNode n in doc.ChildNodes)
{
if (n.Name == "int")
{
result = int.Parse(n.InnerText);
break;
}
}
}
}
}
j lPosted Feb 12, 2010, 5:32 PM
j lPosted Feb 12, 2010, 5:31 PM
Once I parse the xml for the result "2" , then I want to compare it the the "Result" column in the CSV file...
So essentially xml parsed string "2" == Result string "2" in CSV
How does the code look for this?
thanks !
Kirtan PatelPosted Feb 12, 2010, 5:20 PM
int result;
string XMLDataGotFromWebService = ";
//Write data in XML File for Parsing
File.WriteAllText("temp.xml", XMLDataGotFromWebService);
//Load the File and Parse the Needed thing
XmlDocument doc = new XmlDocument();
doc.Load("temp.xml");
foreach (XmlNode n in doc.ChildNodes)
{
if (n.Name == "int")
{
result= int.Parse(n.InnerText);
break;
}
}
j lPosted Feb 12, 2010, 5:12 PM
I'd really like to parse the xml OR be able to use a rule to generate the C# code.
Once I have the web service result "2", I will compare it to an expected value "2" loaded from a CSV spreadsheet.
Example:
Web Service result == (this.Context["mathdata.MathData#csv.Result"")].ToString());