来自Web服务的文本块数据

时间:2012-03-15 10:18:56

标签: c# windows-phone-7

我需要从网络服务获取数据&把它们放在一个文本块中。通过下一个代码,它给我空文本块我的代码有任何问题 ????

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

2 个答案:

答案 0 :(得分:0)

您没有调用DownloadStringAsync对象的inf。您未在e中使用inf_DownloadStringCompleted参数。

答案 1 :(得分: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;
  }