如何从WCF服务获取XML输出

时间:2016-09-14 12:49:31

标签: c# xml wcf

我想得到以下给定的xml作为WCF服务的输出。

import java.util.*;

public class SortUtil {
    public static void main(String[] args) {
        List<Integer> indicies = Arrays.asList(3, 2, 4);
        List<Integer> values   = Arrays.asList(8, 2, 4);
        List<Integer> sorted   = doSort(indicies, values, new TupleComparator<Integer>(0, 1) {
            @Override
            public int doCompare(Integer valueA, Integer valueB) {
                return Integer.compare(valueA, valueB);
            }
        });

        System.out.println(sorted); // [2, 8, 4]
    }

    public static List<Integer> doSort(List<Integer> listA, List<Integer> listB, TupleComparator<Integer> comparator) {
        List<Tuple<Integer>> tuples = zip(listA, listB);
        Collections.sort(tuples, comparator);
        return unzip(tuples, comparator.getValuePos());
    }

    private static <E> List<E> unzip(List<Tuple<E>> tuples, int index) {
        List<E> list = new ArrayList<E>();
        for (Tuple<E> tuple : tuples) {
            list.add(tuple.getData().get(index));
        }
        return list;
    }

    private static <E> List<Tuple<E>> zip(List<E> listA, List<E> listB) {
        List<Tuple<E>> listC = new ArrayList<Tuple<E>>();
        for (int i = 0; i < listA.size(); i++) {
            listC.add(new Tuple<E>(Arrays.asList(listA.get(i), listB.get(i))));
        }
        return listC;
    }

    private static abstract class TupleComparator<E> implements Comparator<Tuple<E>> {
        private int indexPos;
        private int valuePos;
        public TupleComparator(int indexPos, int valuePos) {
            this.indexPos = indexPos;
            this.valuePos = valuePos;
        }
        public int compare(Tuple<E> left, Tuple<E> right) {
            E indexA = left.getData().get(this.getIndexPos());
            E indexB = right.getData().get(this.getIndexPos());
            return doCompare(indexA,  indexB);
        }
        public int getIndexPos() { return indexPos; }
        public int getValuePos() { return valuePos; }
        public abstract int doCompare(E valueA, E valueB);
    }

    private static class Tuple<T> {
        private List<T> data;
        public Tuple(List<T> data) { this.data = data; }
        public List<T> getData() {return data; }
    }
}

以下是我的WCF服务合同和数据合同: -

<?xml version="1.0" encoding="UTF-8"?>
<payloads>
    <payload>
        <firstname>Sid</firstname>
        <secondname>Singh</secondname>
        <number>1</number>
    </payload>
    <payload>
        <firstname>Deepak</firstname>
        <secondname>Shahi</secondname>
        <number>2</number>
    </payload>
    <payload>
        <firstname>Shorya</firstname>
        <secondname>Garg</secondname>
        <number>3</number>
    </payload>
</payloads>

以下是我的班级: -

[ServiceContract]
public interface IService1
{
    [OperationContract]
    Payload Payloads(Payload Payload);
}

[DataContract(Name = "Payload")]
public class Payload
{
    public string _firstname;
    public string _secondname;
    public string _number;

    [DataMember(Name = "firstname")]
    public string firstname
    {
        get { return _firstname; }
        set { _firstname = value; }
    }

    [DataMember(Name = "secondname")]
    public string secondname
    {
        get { return _secondname; }
        set { _secondname = value; }
    }

    [DataMember(Name = "number")]
    public string number
    {
        get { return _number; }
        set { _number = value; }
    }

}

我没有使用我的WCF获得上面给出的XML中所需的输出。请帮忙。在此先感谢:)

1 个答案:

答案 0 :(得分:2)

您的模型

[XmlRoot(ElementName="payload")]
    public class Payload {
        [XmlElement(ElementName="firstname")]
        public string Firstname { get; set; }
        [XmlElement(ElementName="secondname")]
        public string Secondname { get; set; }
        [XmlElement(ElementName="number")]
        public string Number { get; set; }
    }

    [XmlRoot(ElementName="payloads")]
    public class Payloads {
        [XmlElement(ElementName="payload")]
        public List<Payload> Payload { get; set; }
    }

<强> ISERV

 [OperationContract]
              [WebInvoke(Method = "GET",
                  ResponseFormat = WebMessageFormat.Xml,
                  BodyStyle = WebMessageBodyStyle.Bare,
                  UriTemplate = "GetPay")]
              Payload GetPay();

<强>网络

 public Payload GetPay()
            {
                return new Payload();
            }

经过测试和工作。