簡體   English   中英

來自Web服務的文本塊數據

[英]textblock data from web service

嗨,我需要從Web服務獲取數據並將其放入文本塊中。 在下一個代碼中,它給了我空的文本塊,我的代碼有問題嗎?

    public info()
    {

        InitializeComponent();

        WebClient inf = new WebClient();
        // client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);

       inf.DownloadStringCompleted+=new DownloadStringCompletedEventHandler(inf_DownloadStringCompleted);


        //name.Text = 
    }
    public void inf_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        string pass = mp.passwordBox1.Password;

       string id = mp.tx.Text;
        string url = "http://82.212.89.6:888/mob/resources/stdInfo/authenticate/" +id  + "/" +pass  + "/1/570322308ce1121cba1b93f5acc9ebd4733ef2bca90ef942a2cfa224f0aa08dc/1";

        XElement xx = XElement.Parse(url);
       string m= xx.Element("userId").Value;

       name.Text = m;
       }

您沒有在調用inf對象的DownloadStringAsync 您沒有在inf_DownloadStringCompleted使用e參數。

要使用Web服務來解析數據:

    String baseUri = “your service URI";
    WebClient wc = new WebClient();

    public MainPage()
    {
        InitializeComponent();
    wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler     (wc_downloadstringcompleted);
    // event handler that will handle the ‘downloadstringsompleted’ event

           wc.DownloadStringAsync(new Uri(baseUri));    
    //   this method will download your string URI asynchronously    

    }


 void wc_downloadstringcompleted(Object sender, DownloadStringCompletedEventArgs e)
    {
            // method will get fired after URI download completes
             // writes your every code here

            using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
            {         
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            if (reader.Name = "userId")
                                     string str1 = reader.value();
                            break;
                    }
                }
            }

        name.text = str1;
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM