在 WP7 上陷入网络服务困境
昨天我确实询问了如何在 Windows Phone 7 上使用 Web 服务从复杂的 xml 文件中提取一些数据,但不幸的是我没有得到答案,而且我仍然陷入困境。 这是我编写的 C# 代码,但没有在应用程序的屏幕上显示数据:
public MainPage()
{
InitializeComponent();
SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;
Uri url = new Uri("http://www.google.com/ig/api?weather=paris", UriKind.Absolute);
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.DownloadStringAsync(url);
}
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
ListBoxItem areaItem = null;
StringReader stream = new StringReader(e.Result);
XmlReader reader = XmlReader.Create(stream);
string day = String.Empty;
string areaName = String.Empty;
string low = String.Empty;
string high = String.Empty;
string condition = String.Empty;
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case ("day_of_week"):
{
if (true == reader.MoveToFirstAttribute())
{
reader.MoveToContent();
day = reader.ReadElementContentAsString();
day = reader.Value.ToString();
areaItem = new ListBoxItem();
areaItem.Content = day;
listBox1.Items.Add(areaItem);
}
} break;
case ("low"):
{
if (true == reader.MoveToFirstAttribute())
{
reader.MoveToContent();
low = reader.ReadElementContentAsString();
low = reader.Value.ToString();
areaItem = new ListBoxItem();
areaItem.Content = low;
listBox1.Items.Add(areaItem);
}
} break;
case ("high"):
{
if (true == reader.MoveToFirstAttribute())
{
reader.MoveToContent();
high = reader.ReadElementContentAsString();
high = reader.Value.ToString();
areaItem = new ListBoxItem();
areaItem.Content = high;
listBox1.Items.Add(areaItem);
}
} break;
case ("condition"):
{
if (true == reader.MoveToFirstAttribute())
{
reader.MoveToContent();
condition = reader.ReadElementContentAsString();
condition = reader.Value.ToString();
areaItem = new ListBoxItem();
areaItem.Content = condition;
listBox1.Items.Add(areaItem);
}
} break;
}
}
}
}
}
}
}
yesterday i did ask about how to extract some data from a complicated xml file , using web service, on the Windows Phone 7 , but unfortuntly i did not get an answer, and i'm still stuck.
This is the c# code i wrote and did not display the data on my application's screen:
public MainPage()
{
InitializeComponent();
SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;
Uri url = new Uri("http://www.google.com/ig/api?weather=paris", UriKind.Absolute);
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.DownloadStringAsync(url);
}
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
ListBoxItem areaItem = null;
StringReader stream = new StringReader(e.Result);
XmlReader reader = XmlReader.Create(stream);
string day = String.Empty;
string areaName = String.Empty;
string low = String.Empty;
string high = String.Empty;
string condition = String.Empty;
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case ("day_of_week"):
{
if (true == reader.MoveToFirstAttribute())
{
reader.MoveToContent();
day = reader.ReadElementContentAsString();
day = reader.Value.ToString();
areaItem = new ListBoxItem();
areaItem.Content = day;
listBox1.Items.Add(areaItem);
}
} break;
case ("low"):
{
if (true == reader.MoveToFirstAttribute())
{
reader.MoveToContent();
low = reader.ReadElementContentAsString();
low = reader.Value.ToString();
areaItem = new ListBoxItem();
areaItem.Content = low;
listBox1.Items.Add(areaItem);
}
} break;
case ("high"):
{
if (true == reader.MoveToFirstAttribute())
{
reader.MoveToContent();
high = reader.ReadElementContentAsString();
high = reader.Value.ToString();
areaItem = new ListBoxItem();
areaItem.Content = high;
listBox1.Items.Add(areaItem);
}
} break;
case ("condition"):
{
if (true == reader.MoveToFirstAttribute())
{
reader.MoveToContent();
condition = reader.ReadElementContentAsString();
condition = reader.Value.ToString();
areaItem = new ListBoxItem();
areaItem.Content = condition;
listBox1.Items.Add(areaItem);
}
} break;
}
}
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使一切变得过于复杂,它非常简单,使用等等
,
您从这里开始所做的就是将数据绑定到您的 XAML 数据模板
此处文章
You are overcomplicating everything, its really simple, use the
and so on
all you do from here on is Bind the Data to your XAML data template
Article Here