Monday, January 19, 2009

Read A Xml Document With Silverlight(XLINQ)

private void btnSend_Click(object sender, RoutedEventArgs e)
{
string uri = Application.Current.Host.Source.AbsoluteUri;
int index = uri.IndexOf("/ClientBin");
uri = uri.Substring(0, index);
txtAd.Text = uri;
WebClient client = new WebClient();
client.DownloadStringAsync(new Uri(uri+"/data.xml"));
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
}

okuma tamamlandığında ise;
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
try
{
XElement element = XElement.Parse(e.Result);
txtAd.Text = (from x in element.Descendants("musteri")
select x.Element("ad").Value).First();
}
catch (Exception ex)
{
System.Windows.Browser.HtmlPage.Window.Alert(ex.InnerException.Message);
}
}

No comments:

Post a Comment