[英]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.