通过和读取XML到Web服务 [英] PASS and Read XML to Webservice
本文介绍了通过和读取XML到Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何创建读取XML文件的Web服务,以及如何将XML传递给此Web服务。
当我作为字符串传递时,它给出以下错误
System.Web.HttpRequestValidationException:有潜在危险从客户端检测到Request.Form值(OrderSummaryID ="< AllPlayers>< Player ...")。
解决方案
您可以创建类似的东西以下,这是快速和脏,使用basicHttpBinding
在服务方面:
[ServiceContract]
公共接口ICrudService
{
[OperationContract]
DataTable GetEmployeeData();
[OperationContract]
bool DoWork(DataTable empData);
}
公共类CrudService:ICrudService
{
public bool DoWork(DataTable empData)
{
//执行任何数据库活动
返回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 ;我< 10; i ++)
{
随机r =新随机(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();
返回dtEmp;
}
}
和ASPX页面上您要使用该服务:
private CrudServiceClient _proxy = new CrudServiceClient(BasicHttpBinding_ICrudService);
公共DataTable员工
{
get
{
if(ViewState) [Emp]!= null)
return(DataTable)ViewState [Emp];
else
返回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();
}
其他
{
_proxy = new CrudServiceClient();
this.Employees = _proxy.GetEmployeeData();
}
if(this.Employees!= null)
{
lblMsg.Text = string.Format(系统中的总{0}员工,this.Employees.Rows.Count.ToString());
btnUpdate.Enabled = true;
}
其他
{
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 ] =琼斯;
newEmp [薪资] = 50000;
dtEmp.Rows.Add(newEmp);
}
if(_proxy!= null)
{
if(_proxy.DoWork(dtEmp))< br $>
{
lblMsg.Text =员工成功更新;
}
}
else
{
_proxy = new CrudServiceClient();
if(_proxy.DoWork(dtEmp) ))
{
lblMsg.Text =员工成功更新;
}
}
btnUpdate.Enabled = false;
btnGet.Enabled = true;
this.Employees = null;
}
HTH,
谢谢
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 ...").
解决方案
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
这篇关于通过和读取XML到Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文