Define WCF XML response schema

不打扰是莪最后的温柔 提交于 2019-12-24 00:22:53

问题


I build a WCF Rest service to provide data for another process. suppose that his name is GetData. This one provide a xml response having this structure :

<?xml version="1.0" encoding="utf-8"?>
<GetDataResponse xmlns="http://tempuri.org/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <GetDataResult>
    <DataMessage>   
      <a></a>
      <b></b>
      <c></c>
    </DataMessage>
  </GetDataResult>
</GetDataResponse>

the service interface :

    [XmlSerializerFormat]
    [OperationContract(Name = "GetData")]
    [WebInvoke(Method = "GET",
               ResponseFormat = WebMessageFormat.Xml,
               BodyStyle = WebMessageBodyStyle.Wrapped,
               UriTemplate = "Data/{Param}")]
    List<DataMessage> GetData(string Params);

I would like to deserialize the xml after saving it, following the DataMessage class. So, I would like to have this schema :

<?xml version="1.0" encoding="utf-8"?>
<DataMessages xmlns="http://tempuri.org/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <DataMessage>
      <a></a>
      <b></b>
      <c></c>
    </DataMessage>
</DataMessages>

What should I do to define the service response schema to have it like this?

Thank you.


回答1:


You can use some attributes in the System.Xml.Serialization namespace to define an object graph which maps to the schema you have. The code below does that.

public class StackOverflow_7905186
{
    [XmlType(TypeName = "DataMessage", Namespace = "http://tempuri.org/")]
    public class DataMessage
    {
        public string a;
        public string b;
        public string c;
    }
    [XmlRoot(ElementName = "DataMessages", Namespace = "http://tempuri.org/")]
    public class DataMessages
    {
        [XmlElement(ElementName = "DataMessage")]
        public List<DataMessage> Messages;
    }
    [ServiceContract]
    public class Service
    {
        [XmlSerializerFormat]
        [OperationContract(Name = "GetData")]
        [WebGet(ResponseFormat = WebMessageFormat.Xml,
                BodyStyle = WebMessageBodyStyle.Bare,
                UriTemplate = "Data/{Param}")]
        [return: MessageParameter(Name = "DataMessages")]
        public DataMessages GetData(string Param)
        {
            return new DataMessages
            {
                Messages = new List<DataMessage>
                {
                    new DataMessage
                    {
                        a = "1",
                        b = "2",
                        c = "3",
                    }
                }
            };
        }
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
        host.Open();
        Console.WriteLine("Host opened");

        WebClient c = new WebClient();
        Console.WriteLine(c.DownloadString(baseAddress + "/Data/foo"));

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}


来源:https://stackoverflow.com/questions/7905186/define-wcf-xml-response-schema

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!