WebService从序列化XML返回JSON,但类似于字符串

时间:2019-02-07 12:40:03

标签: c# json wcf

我有自托管的Web服务。我收到请求(JSON)。我对此进行处理,并得到类似XmlDocument的结果。现在,我需要返回响应(也是JSON)。但是有一个问题,因为结果是JSON结构,但是是字符串格式。

[ServiceContract(Name = "MyService", Namespace = "http://tempuri.org/")]
public interface ImyAgent
{
    [OperationContract]
    [WebInvoke(UriTemplate = "json-post", Method = "*", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    string ActionJsonPost(string data);
}

namespace MyService
{
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single, IncludeExceptionDetailInFaults = true, AddressFilterMode = AddressFilterMode.Any)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class myAgent : ImyAgent
    {
        public myAgent() { }

        public string ActionJsonPost(string data)
        {
            XmlDocument xml = new XmlDocument();
            xml.LoadXml("<root><question>language</question><date>2019-02-07</date></root>"); //some XML structure, I can't specified, depends on requests
            return JsonConvert.SerializeXmlNode(xml);
        }
    }
}

因此,XML结果是:

  

语言 <日期> 2019-02-07

JSON结果应为:

  

{“ root”:{“ question”:“ language”,“ date”:“ 2019-02-07”}}

但不幸的是,实际结果是:

  

“ {\” root \“:{\”问题\“:\”语言\“,\”日期\“:\” 2019-02-07 \“}}}”

0 个答案:

没有答案