我有一个 Web 服务函数,它返回一个包含 6 个条目的列表,它使用自定义结构。我的客户端项目上的引用更新,但返回的列表虽然具有正确的条目数,但所有数据字段未列出并替换为一个名为“PropertyChanged”的字段,该字段为空。
我的猜测是我缺少正确序列化它的东西,这是我的代码:
结构:
public struct TreeData
{
private readonly string text;
private readonly string parent;
private string val;
public TreeData(string Text, string Parent)
{
this.text = Text;
this.parent = Parent;
this.val = "";
}
public TreeData(string Text, string Parent, string Value)
{
this.text = Text;
this.parent = Parent;
this.val = Value;
}
[XmlText()] public string Text { get { return text; } }
[XmlText()] public string Parent { get { return parent; } }
[XmlText()] public string Value { get { return val; } }
}
网络方法(简化):
[System.Xml.Serialization.XmlInclude(typeof(TreeData))]
[WebMethod]
public List<TreeData> getList()
{
List<TreeData> myList = new List<TreeData>();
myList.Add(new TreeData("one", "two"));
return myList;
}
客户端功能:
protected void Page_Load(object sender, EventArgs e)
{
userName = Session["UserName"].ToString();
using (ServiceReference1.Service1SoapClient myService = new ServiceReference1.Service1SoapClient())
{
Demogs = userinfo[0];
Agencies = userinfo[1];
List<TreeData> treeNodes = myService.LoadSites( Demogs, Agencies, true).ToList();
DataTable table = myService.getTable();
myGrid.DataSource = table;
myGrid.DataBind();
}
}
所以在 webmethod 返回之前,列表看起来是正确的:
myList Count = 3
[0] parent= "one" text = "two" value = "three"
[1] parent= "1" text = "2" value = "3"
[2] parent= "a" text = "b" value = "c"
返回并分配给 treeNodes 后,它看起来像:
TreeNodes Count = 3
[0] PropertyChanged = null;
[1] PropertyChanged = null;
[2] PropertyChanged = null;