xml反序列化<string>标记

时间:2016-07-04 13:34:09

标签: c# xml deserialization

我使用System.ServiceModel System.ServiceModel.Web System.ServiceModel.Activation名称空间编写了Web服务。

[XmlSerializerFormat]
        [OperationContract]
        [WebGet(UriTemplate = Routing.GetClientRoute, BodyStyle = WebMessageBodyStyle.Bare)]
        string GetClientNameById(string Id);           

我用这个类描述了数据:

[XmlRoot("ServiceXmlReply")]
    public class ServiceXmlReply
    {
        [XmlElement]
        public string Name;
    }

问题在于我从服务中收到的回复。它看起来像这样:

"<?xml version=\"1.0\" encoding=\"utf-8\"?>
<string>
<ServiceXmlReply xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">
<Name>Igor<Name>
<ServiceXmlReply>
</string>"

你可以看到那里不应该是&#34; string&#34;标签,但它是。因此,我无法反序列化响应并获取数据。

服务器和客户端的类是相同的。

1 个答案:

答案 0 :(得分:0)

您的操作合同定义GetClientNameById的响应是string类型。所以会发生什么是返回类型字符串的内容是序列化的xml。

要实现这一目标,您应该:

  1. 将操作的返回类型更改为ServiceXmlReply或
  2. 放弃使用ServiceXmlReply。在服务和客户端使用字符串。