【发布时间】:2016-04-29 15:02:42
【问题描述】:
我有一个运行正常的 Web 服务(在 iis 中运行的 asmx)。唯一的问题是客户端对 XML 输出非常严格。当前格式如下:
<ArrayOfVenda xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://213.63.189.121/webservicenos">
<venda>
<id>x</id>
<contact_moment>x</contact_moment>
</venda>
<venda>
<id>y</id>
<contact_moment>y</contact_moment>
</venda>
</ArrayOfVenda>
它应该是:
<ArrayOfVenda xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://213.63.189.121/webservicenos">
<root>
<venda>
<id>x</id>
<contact_moment>x</contact_moment>
</venda>
<venda>
<id>y</id>
<contact_moment>y</contact_moment>
</venda>
</root>
</ArrayOfVenda>
所以唯一的事情就是添加一个名为 root 的 XMLElement,其中包含列表 venda。我在添加这个元素时遇到了麻烦,但我真的不知道如何在我的代码中处理它。这里是:
[WebMethod]
[return: System.Xml.Serialization.XmlElementAttribute("venda")]
public List<venda> getListaVendas(string dt_min, string dt_max)
{
List<venda> objVendaList = new List<venda>();
using (SqlConnection con = new SqlConnection(@"Data Source=server;Initial Catalog=db;User ID=user;password=password"))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM dbo.vcnosadesoes_getlistavendas where contact_moment >='" + dt_min + "' AND contact_moment <DATEADD(dd, 1, '" + dt_max + "')", con))
{
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
var objVenda = new venda();
objVenda.id = dr["id"].ToString();
objVenda.contact_moment = dr["contact_moment"].ToString();
objVenda.nome = dr["nome"].ToString();
objVenda.pacote = dr["pacote"].ToString();
objVenda.telefone = dr["telefone"].ToString();
objVenda.codigo_wc = dr["codigo_wc"].ToString();
objVendaList.Add(objVenda);
}
dr.Close();
}
}
return objVendaList;
}
任何想法添加此元素的最佳方法是什么?
PS:我知道。由于 SQL 注入,我必须更改 SQL 查询,我会在将其上线之前先了解它,不要担心。还有这条线:
[return: System.Xml.Serialization.XmlElementAttribute("venda")]
可能什么都不做,我只是把它放在一些测试上,从不评论它。
更新:所以来自客户端的脚本仍然返回错误。在查看调试器几个小时后,我发现它需要的是这个输出:
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://213.63.189.121/webservicenos">
<venda>
<id>x</id>
<contact_moment>x</contact_moment>
</venda>
<venda>
<id>y</id>
<contact_moment>y</contact_moment>
</venda>
</root>
【问题讨论】:
标签: c# xml web-services