来自Webservice的Xml输出添加一个Xml元素C#

时间:2016-04-29 15:02:43

标签: c# xml web-services

我有一个没有问题的web服务(asmx在iis中运行)。唯一的问题是客户端对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>

1 个答案:

答案 0 :(得分:0)

由于XML具有额外的测试级别(<root><ArrayOfVenda>之间的<venda>元素),因此返回的c#类型必须具有类似的结构才能成功序列化形成。因此,引入新的根类型ArrayOfVenda

[XmlRoot(ElementName = "ArrayOfVenda", Namespace = "http://213.63.189.121/webservicenos")]
public class ArrayOfVenda
{
    [XmlArray("root")]
    [XmlArrayItem("venda")]
    public List<venda> VendaList { get; set; }
}

从您的getListaVendas方法中返回:

    public ArrayOfVenda getListaVendas(string dt_min, string dt_max)
    {
        List<venda> objVendaList = new List<venda>();

        // Fill in objVendaList as you do currently

        return new ArrayOfVenda { VendaList = objVendaList };
    }

<强>更新

鉴于您对XML输出的新要求,您可以按如下方式定义ArrayOfVenda类:

[XmlRoot(ElementName = "root", Namespace = "http://213.63.189.121/webservicenos")]
public class ArrayOfVenda
{
    [XmlElement("venda")]
    public List<venda> VendaList { get; set; }
}

[XmlRoot(...)]定义ArrayOfVenda作为根XML元素时的名称和名称空间。 [XmlElement("venda")]表示VendaList的格式没有外部容器元素,并且每个项目都将被命名为<venda>