【问题标题】:WCF Serialization Return - NoobWCF 序列化返回 - Noob
【发布时间】:2010-12-16 02:15:02
【问题描述】:

我有一个对象,我很好地将其序列化为:

<?xml version="1.0" encoding="utf-8" ?> 
<people xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" userID="AX12345">
  <group groupID="1234_ABCD">
    <person name="Name 0" id="0" /> 
    <person name="Name 1" id="1" /> 
    <person name="Name 2" id="2" /> 
    <person name="Name 3" id="3" /> 
  </group>
</people>

作为字符串返回到 this:

    [OperationContract]
    [WebGet(UriTemplate = "format/{format}/userid/{userid}/sessionkey/{sessionkey}")]
    string Get(string format,string userid,string sessionkey);

当我查看这个服务返回的数据时,我得到了这个。

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header />
  <s:Body>
    <GetResponse xmlns="http://tempuri.org/">
      <GetResult>**&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;people xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" userID="123BOBBY"&gt;&lt;group groupID="1234_ABCD"&gt;&lt;person name="Name 0" id="0" /&gt;&lt;person name="Name 1" id="1" /&gt;&lt;person name="Name 2" id="2" /&gt;&lt;person name="Name 3" id="3" /&gt;&lt;/group&gt;&lt;/people&gt;</**GetResult>
    </GetResponse>
  </s:Body>
</s:Envelope>

而我想要的是这样的:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header />
  <s:Body>
    <GetResponse xmlns="http://tempuri.org/">
      <GetResult>
        <people userID="AX12345">
          <group groupID="1234_ABCD">
            <person name="Name 0" id="0" /> 
            <person name="Name 1" id="1" /> 
            <person name="Name 2" id="2" />
            <person name="Name 3" id="3" /> 
          </group>
        </people>
      </GetResult>
    </GetResponse>
  </s:Body>
</s:Envelope>

我是这一切的初学者。

【问题讨论】:

    标签: wcf serialization


    【解决方案1】:

    您可以尝试直接从方法中返回它并让 WCF 处理序列化,而不是序列化对象:

    [OperationContract]
    [WebGet(UriTemplate = "format/{format}/userid/{userid}/sessionkey/{sessionkey}")]
    YourObject Get(string format,string userid,string sessionkey);
    

    【讨论】:

    • 您好 - 感谢您如此迅速地回复。我试图序列化的原因是 WCF 将返回的对象包装在它自己的标记方案中 - 它在所有数据标记之前都以
    • 嗯 - 我发现了这个:msdn.microsoft.com/en-us/magazine/cc163569.aspx 我会检查一下并发布我的发现 - 看起来它可能对我有用 - 睡前阅读 12 页 ;)
    【解决方案2】:

    原来答案相当简单。

    [XmlSerializerFormat]
    [OperationContract]
    [WebGet(UriTemplate = "format/{format}/userid/{userid}/sessionkey/{sessionkey}")]
    string Get(string format,string userid,string sessionkey);
    

    DataContractSerializer 是默认的序列化器,[XmlSerializerFormat] 会覆盖序列化。

    在我的课程中,我添加了序列化属性:

    using System;
    using System.Collections.Generic;
    using System.Xml.Serialization;
    
    namespace Myapp
    {
        [XmlRoot("people")]
        public class People
        {
            private string strUserID = "";
            private List<Group> lstGroup;
    
            public People()
            {
               lstGroup = new List<Group>(); 
            }
    
            [XmlAttribute("userID")]
            public string UserID
            {
                get { return strUserID; }
                set { strUserID = value; }
            }
            [XmlElement("group")]
            public List<Group> Group
            {
                get { return lstGroup; }
                set { lstGroup = value; }
            }
    
         }
    }
    

    虽然我对短期收益感到满意,但我会更深入地研究这一点,以确保我能控制输出。

    谢谢

    P

    【讨论】:

      猜你喜欢
      • 2012-02-19
      • 1970-01-01
      • 1970-01-01
      • 2011-09-23
      • 1970-01-01
      • 2011-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多