[英]WCF - same name tags in xml response
我是新手。 請Plzz幫我。 在WCF rest應用程序中,我需要以下響應
<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個相同的“選擇”標簽,但內容不同。 我得到的只是最后一個“選擇”標簽。
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "/getQuestion")]
[return: MessageParameter(Name = "parameterList")]
List<parameter> getQuestion();
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;
}
public class parameter
{
public string Question
{
get{ } set{ }
}
public string Choice
{
get{ } set{ }
}
}
解決了
[XmlElement("Choice")]
public List<string> Choice
{
get { return aChoice; }
set { aChoice = value; }
}
只需在屬性上方添加[XmlElement(“”)]以重命名Xml元素。
謝謝詹姆斯。 你很有幫助。
您繼續使用新字符串覆蓋Choice。 您的Choice屬性必須是列表
public List<string> Choice
{
get { return new List<string>();}
}
然后您可以將其添加到列表中
param.Choice.add(" Occupation ");
param.Choice.add(" Retired/housewife ");
等等....
之后,您將不得不遍歷所有選擇並將它們添加到XML中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.