Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi,

How to create a webservice which reads XML file, And how to passes XML to this webservice.

When i pass as string its giving below error

System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client (OrderSummaryID="<AllPlayers><Player ...").
Posted
Comments
TheKarateKid 18-Feb-15 12:51pm    
The XML string you are trying to pass is a XML Fragment or XML Document?
you can have WebMethod/OperationContract with DataSet or DataTable as a method parameter. pass the DataSet or DataTable directly to WebMethod while invoking it from proxy
Sanju TV 18-Feb-15 13:06pm    
can i have any sample application

1 solution

You can create something like following, this is quick and dirty, with basicHttpBinding

On the service side :

[ServiceContract]
public interface ICrudService
{
[OperationContract]
DataTable GetEmployeeData();

[OperationContract]
bool DoWork(DataTable empData);
}

public class CrudService : ICrudService
{


public bool DoWork(DataTable empData)
{
//Perform any db activities

return true;
}

public DataTable GetEmployeeData()
{
DataTable dtEmp = new DataTable("EmpData");

dtEmp.Columns.Add("ID");
dtEmp.Columns.Add("FirstName");
dtEmp.Columns.Add("LastName");
dtEmp.Columns.Add("Salary");

for (int i = 0; i < 10; i++)
{
Random r = new Random(10);

DataRow row = dtEmp.NewRow();
row["ID"] = i + 1;
row["FirstName"] = "John " + (i + 1).ToString();
row["LastName"] = "Doe";
row["Salary"] = 10000.00 + r.Next();

dtEmp.Rows.Add(row);
}
dtEmp.AcceptChanges();


return dtEmp;
}
}

and on the ASPX Page where you want to consume the service:

private CrudServiceClient _proxy = new CrudServiceClient("BasicHttpBinding_ICrudService");

public DataTable Employees
{
get
{
if (ViewState["Emp"] != null)
return (DataTable)ViewState["Emp"];
else
return null;
}
set
{
ViewState["Emp"] = value;
}
}

protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnGet_Click(object sender, EventArgs e)
{
if (_proxy != null)
{
this.Employees = _proxy.GetEmployeeData();
}
else
{
_proxy = new CrudServiceClient();
this.Employees = _proxy.GetEmployeeData();
}

if (this.Employees != null)
{
lblMsg.Text = string.Format("Total {0} Employees in the System", this.Employees.Rows.Count.ToString());
btnUpdate.Enabled = true;
}
else
{
btnUpdate.Enabled = false;
}


}

protected void btnUpdate_Click(object sender, EventArgs e)
{
DataTable dtEmp = this.Employees.Clone();

if (this.Employees != null)
{
DataRow newEmp = dtEmp.NewRow();
newEmp["ID"] = 11;
newEmp["FirstName"] = "Jonny";
newEmp["LastName"] = "Jones";
newEmp["Salary"] = 50000;
dtEmp.Rows.Add(newEmp);
}

if (_proxy != null)
{
if (_proxy.DoWork(dtEmp))
{
lblMsg.Text = "Employee Updated Successfully";
}

}
else
{
_proxy = new CrudServiceClient();
if (_proxy.DoWork(dtEmp))
{
lblMsg.Text = "Employee Updated Successfully";
}

}


btnUpdate.Enabled = false;
btnGet.Enabled = true;
this.Employees = null;
}

HTH,
Thanks
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900