0

カスタム構造体を使用する 6 つのエントリを含むリストを返す Web サービス関数があります。クライアント側のプロジェクトで参照が更新されますが、返されたリストには正しい数のエントリが含まれていますが、リストされていないすべてのデータ フィールドが含まれており、null である "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; } }

}

Web メソッド (簡略化):

 [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;
4

1 に答える 1

0

シリアライゼーション属性を混同している可能性があると思います。WCF Web サービスを使用している場合は、DataContractおよびDataMember属性を使用して、構造体のどのメンバーを含めるかを指定する必要がある場合があります。

于 2013-08-28T22:04:48.293 に答える