Opening a remote XML file

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.

Getting ready

  1. Create a new project called Recipe3_RemoteXml under Ch3_Recipes.
  2. Open your local IIS (Internet Information Services) root folder and copy the XML file MyTasks.xml we created in the preceding recipe.
  3. You can also host this file in any remote server, which can be accessed using a simple URI. For this recipe, let's copy the XML file to localhost's root folder. You can learn more on how to install the IIS on your machine using this link: http://learn.iis.net/page.aspx/28/installing-iis-7-on-windows-vista-and-windows-7/.
  4. Navigate to the file using the browser and the following link http://localhost/mytasks.xml. It should look like the following screenshot:
    Getting ready

How to do it…

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.

  1. Open the MainPage.xaml file and rename the app to RemoteXML.
  2. Add a 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>
    
  3. Now add another 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>
    
  4. Now open the 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);
    }
    
  5. Add the event handler method 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;
    }
    
  6. Now let's add the button click event method. Here we format the URI text in the textbox control to 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);
    }
    
  7. Press F5 and type in the URL of the 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:
    How to do it…

How it works...

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.

There's more...

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.

See also

Check Chapter 6, Representational State Transfer, to understand more on how REST-style web services work.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset