【问题标题】:WCF - same name tags in xml responseWCF - xml响应中的同名标签
【发布时间】:2012-07-26 20:00:33
【问题描述】:

我是新手。请在这方面帮助我。 在 WCF 休息应用程序中,我想要以下响应

<parameterList>    
        <parameter>        
            <Question> Occupation </Question> 

            <Choice> Student/Others </Choice>      
            <Choice> Retired/housewife </Choice>       
            <Choice> Salaried/SelfEmployed </Choice>        
            <Choice> Doctor/CA/Socially Important Person </Choice> 

        </parameter>    
</parameterList>

我想要 4 个具有不同内容的相同“选择”标签。我得到的只是最后一个“选择”标签。

IService.cs

[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "/getQuestion")]
[return: MessageParameter(Name = "parameterList")]
List<parameter> getQuestion();

Service.svc.cs

public List<parameter> getQuestion()
    {
        List<parameter> lstParameter = new List<parameter>();
        parameter param = new parameter();
        param.Question = " Occupation ";
        param.Choice = " Student/Others ";
        param.Choice = " Retired/housewife ";
        param.Choice = " Salaried/SelfEmployed ";
        param.Choice = " Doctor/CA/Socially Important Person ";
        lstParameter.Add(param);
        return lstParameter;
    }

parameter.cs

public class parameter
{
    public string Question
    {
        get{ } set{ }
    }

    public string Choice
    {
        get{ } set{ }
    }
}

【问题讨论】:

    标签: xml wcf rest response


    【解决方案1】:

    解决了

            [XmlElement("Choice")]
    
            public List<string> Choice
            {
                get { return aChoice; }
                set { aChoice = value; }
            }
    

    只需在属性上方添加 [XmlElement("")] 即可重命名 Xml 元素。

    感谢詹姆斯。你很有帮助。

    【讨论】:

      【解决方案2】:

      您不断用新字符串覆盖 Choice。您的 Choice 属性必须是 List

         public List<string> Choice
              {
                  get { return new List<string>();}
              }
      

      然后你可以添加到列表中

      param.Choice.add(" Occupation ");
      param.Choice.add(" Retired/housewife ");
      

      等等……

      之后,您必须遍历所有选项并将它们添加到您的 XML 中

      【讨论】:

      • 感谢您的回复.. 使用您的建议后,我得到 ' Choice 1 Choice 2 Choice 2 string> Choice 2 ' 但我想要 ' Choice 1 Choice 2 Choice 3 选择 4 ' 请帮助..
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-03
      • 1970-01-01
      • 2011-01-30
      • 1970-01-01
      相关资源
      最近更新 更多