定义 WCF XML 响应架构 [英] Define WCF XML response schema

查看:19
本文介绍了定义 WCF XML 响应架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个 WCF Rest 服务来为另一个进程提供数据.假设他的名字是 GetData.这提供了一个具有这种结构的 xml 响应:

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>

服务接口:

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

我想在保存后反序列化 xml,遵循 DataMessage 类.所以,我想要这个架构:

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?

谢谢.

推荐答案

您可以使用 System.Xml.Serialization 命名空间中的一些属性来定义映射到您拥有的架构的对象图.下面的代码就是这样做的.

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

这篇关于定义 WCF XML 响应架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆