如果我有list,如何填写服务请求?

本文关键字:何填写 服务 请求 list Class 如果 | 更新日期: 2023-09-27 18:17:52

请求类结构:

public class QueryParams {
    public List<QueryParam> QueryParam { get; set; }
}
public class QueryParam {
    public string Parameter { get; set; }
}

Service is expected

<typ:queryParams>
    <typ:queryParam>
        <typ:parameter>BUSINESS_CATEGORY</typ:parameter>
    </typ:queryParam>
    <typ:queryParam>
        <typ:parameter>CATEGORY</typ:parameter>
    </typ:queryParam>
</typ:queryParams>

如何形成请求?

如果我有list<Class>,如何填写服务请求?

using QueryParams = System.Collections.Generic.List<QueryParam>;
public class QueryParam {
    public string Parameter { get; set; }
    public QueryParam(string para) {
        Parameter = para;
    }
}
public class Program
{
    public static void Main()
    {
        var Qp = new QueryParams() {
            new QueryParam("BUSINESS_CATEGORY"),
            new QueryParam("CATEGORY")
        };
        string QpXml = ToXml(Qp);
        // Use your XML from here on
    }
    private static string ToXml(QueryParams Qp) {
        StringBuilder Sb = new StringBuilder();
        Sb.AppendLine("<typ:queryParams>");
        foreach (var q in Qp) {
            Sb.AppendLine("<typ:queryParam>");
            Sb.AppendLine("<typ:parameter>" + q.Parameter + "</typ:parameter>");
            Sb.AppendLine("</typ:queryParam>");
        }
        Sb.AppendLine("</typ:queryParams>");
        return Sb.ToString();
    }
}

查看这个。net Fiddle