In the first recipe Opening a local XML file, we opened an XML file from the local folder; now let's try opening an XML file from a remote location.
Recipe3_RemoteXml
under Ch3_Recipes
. MyTasks.xml
we created in the preceding recipe.In the following steps, we will create a form to collect the remote XML file location information in a textbox control, and then using the WebClient
class we will download the file. Once the file download is completed, we will display it in the list box control.
MainPage.xaml
file and rename the app to RemoteXML
. TextBox
control to collect the remote XML file and a Button
control inside the ContentPanel
grid:<!--TitlePanel contains the name of the application and page title--> <StackPanel x:Name="TitlePanel"Grid.Row="0" Margin="12,17,0,28"> <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResourcePhoneTextNormalStyle}"/> <TextBlock x:Name="PageTitle" Text="Remote Xml" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> </StackPanel> <!--ContentPanel - place additional content here--> <Grid x:Name="ContentPanel"Grid.Row="1" Margin="12,0,12,0"> <TextBox Height="72"HorizontalAlignment="Left" Margin="0,52,0,0" Name="textBox1" Text=""VerticalAlignment="Top" Width="460" /> <TextBlock Height="30"HorizontalAlignment="Left" Margin="14,32,0,0" Name="textBlock1" Text="Remote Xml File Location:"VerticalAlignment="Top" /> <Button Content="Go" Height="72"HorizontalAlignment="Left" Margin="0,116,0,0" Name="button1"VerticalAlignment="Top" Width="160" Click="button1_Click" /> </Grid>
Grid
following the preceding grid to display the result just like we did in the preceding recipe:<!--ContentPanel - place additional content here--> <Grid x:Name="ContentPanel2" Margin="12,217,12,-13"Grid.Row="1"> <ListBox x:Name ="lstTasks" Margin="0,6,0,0"> <ListBox.ItemTemplate> <DataTemplate> <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> <RowDefinition Height="15" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="150" /> <ColumnDefinition Width="200" /> <ColumnDefinition Width="100" /> </Grid.ColumnDefinitions> <TextBlockGrid.Row="0"Grid.Column="0" Text="{Binding Name}"FontWeight="Bold" Foreground="OrangeRed"/> <TextBlockGrid.Row="0"Grid.Column="1" Text="{BindingDateDue}" /> <TextBlockGrid.Row="0"Grid.Column="2" Text="{Binding Priority}" Foreground="Yellow"/> <TextBlockGrid.Row="1"Grid.ColumnSpan="3" Text="{Binding Notes}" /> <TextBlockGrid.Row="2"Grid.ColumnSpan="3" /> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid>
MainPage.xaml.cs
file and add the following lines of code to download the XML file from the remote location using the WebClient
class:WebClientremoteXml; PublicMainPage() { InitializeComponent(); remoteXml = new WebClient(); remoteXml.DownloadStringCompleted += new DownloadStringCompletedEventHandler (remoteXml_DownloadStringCompleted); }
remoteXml_DownloadStringCompleted
, which parses the XML document:Private void remoteXml_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { XDocumentxdoc = XDocument.Parse(e.Result); IEnumerable<DataClass>iTasks; iTasks = from task inxdoc.Descendants("task") selectnewDataClass { Name = (string)task.Attribute("name"), Notes = (string)task.Attribute("notes"), Priority = (string)task.Attribute("priority"), DateDue = (DateTime)task.Attribute("datedue"), DateCreated = (DateTime)task.Attribute("datecreated") }; lstTasks.ItemsSource = iTasks; }
Uri
and then call the DownloadStringAsync
method to fetch the XML file from the remote location:private void button1_Click(object sender, RoutedEventArgs e) { stringtxtUri = textBox1.Text; txtUri = Uri.EscapeUriString(txtUri); Uriuri = newUri(txtUri); remoteXml.DownloadStringAsync(uri); }
MyTasks.xml
file. When you click on Go, you should see the parsed XML content displayed in the list box control, as shown in the following screenshot:We first declared WebClient
and then added the event handler. Using WebClient
is the easiest way to access remote files. As it is asynchronous, we have to wait for the download to complete. We added the download event handler.
In the button click event method, we converted the text from the textbox to escape the Uri string. Then, we got all the task child elements using the Descendants
method. Finally, the resulting list was bound to the list box control.
We can add more features like saving the XML file to local storage or saving the URI string to our local storage settings file. Refer to MSDN for more information on LINQ to XML at the following link: http://msdn.microsoft.com/en-us/library/bb425822.aspx.
Check Chapter 6, Representational State Transfer, to understand more on how REST-style web services work.