2012-03-15 70 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; 
    }