2012-03-15 73 views
0

嗨,我需要從Web服務獲取數據&把它們放入一個文本塊。通過下一個代碼,它給了我空文本塊是否有任何問題,我的代碼 ?來自web服務的textblock數據

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; 
     } 
+1

你好像離別的網址,而不是它的內容。 – reinierpost 2012-03-15 10:24:31

+0

使用'xmlreader'來讀取結果並解析其內容 – 2012-03-15 10:31:21

+0

我從xmlreader中定義了一個對象,並將它作爲任何類對象調用。 – 2012-03-15 10:38:29

回答

0

你是不是調用inf對象DownloadStringAsync。您在inf_DownloadStringCompleted中未使用e參數。

+0

PLZ但我不明白我該怎麼做?爲什麼我應該使用e – 2012-03-15 10:41:15

+0

@ww Nawras'e.Result'包含下載的數據。更多信息在這裏http://msdn.microsoft.com/en-us/library/system.net.downloadstringcompletedeventargs.aspx – clearpath 2012-03-15 10:54:34

0

使用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; 
    }