如果我有列表<class> </class>,如何形成服务请求

时间:2014-08-13 10:35:36

标签: c# asp.net c#-4.0

请求类结​​构:

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

服务期望以下格式:

<typ:queryParams>

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

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

</typ:queryParams>

我如何形成请求?

1 个答案:

答案 0 :(得分:0)

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();
    }
}

请参阅this .NET Fiddle