by
kirupa | 27 April 2008
In the
previous page,
you added some code to load your XML file, and the
code seemed to work because when you ran it, you saw
a browser alert appear with your XML file's contents
displayed. Let's dig into more detail and learn why
the code worked.
Before
diving into the code, let's take a bird's eye look
at what we are trying to do first. The layout of
your XML file in relation to your XAP is as follows:
In other words, it is
in the same location as your XAP file. Our approach
is as follows:
Once your application has
initialized, we will load our XML file
asynchronously. This means that the rest of your
application isn't blocked waiting for your XML file
to load. Once our XML file has loaded, you read its
contents and present it in the form of a browser
alert.
What I described above
is essentially what our code does. Let's look at the
code line by line starting with the call we make to
set everything in motion:
-
public
Page()
-
{
-
InitializeComponent();
-
- LoadXMLFile();
-
}
The first thing we do
is call a method called
LoadXMLFile directly after we call
InitializeComponent. The InitializeComponent call is
partly responsible for actually ensuring your
application is initialized.
- private
void
LoadXMLFile()
- {
- WebClient
xmlClient
=
new
WebClient();
- xmlClient.DownloadStringCompleted
+=
new
DownloadStringCompletedEventHandler(XMLFileLoaded);
- xmlClient.DownloadStringAsync(new
Uri("sampleXML.xml",
UriKind.RelativeOrAbsolute));
- }
The LoadXMLFile method
contains the code that sets up everything to get
your XML file downloaded. To help with this, you
have the WebClient class. The WebClient class
provides you with a lot of the functionality for
setting up your download, monitoring your download's
progress, and notifying you when the download has
completed.
As you can see, we
create a new WebClient object called
xmlClient to handle
everything related to downloading your XML file:
-
private
void
LoadXMLFile()
- {
- WebClient
xmlClient
=
new
WebClient();
-
xmlClient.DownloadStringCompleted
+=
new
DownloadStringCompletedEventHandler(XMLFileLoaded);
-
xmlClient.DownloadStringAsync(new
Uri("sampleXML.xml",
UriKind.RelativeOrAbsolute));
-
}
Once you have created
your WebClient object, it is time to start setting
up the events with their event handlers. The first
thing I do is specify what will happen when your XML
file has been fully loaded:
-
private
void
LoadXMLFile()
- {
-
WebClient
xmlClient
=
new
WebClient();
- xmlClient.DownloadStringCompleted
+=
new
DownloadStringCompletedEventHandler(XMLFileLoaded);
-
xmlClient.DownloadStringAsync(new
Uri("sampleXML.xml",
UriKind.RelativeOrAbsolute));
- }
The event is called
DownloadStringCompleted, and the auto
complete provides me with the
DownloadStringCompletedEventHandler text
automatically. All I have to do is specify the name
of my
event handler which I call XMLFileLoaded. In
other words, when your download has completed, the
XMLFileLoaded method will get called.
The final line is where you actually specify the
XML file that gets loaded:
-
private
void
LoadXMLFile()
- {
-
WebClient
xmlClient
=
new
WebClient();
-
xmlClient.DownloadStringCompleted
+=
new
DownloadStringCompletedEventHandler(XMLFileLoaded);
- xmlClient.DownloadStringAsync(new
Uri("sampleXML.xml",
UriKind.RelativeOrAbsolute));
- }
In this line, you call your xmlClient object's
DownloadStringAsync method where you specify the
path to your XML file. The path you specify is in
the form a Uri object where you specify the path to
your XML file and whether the path is relative to
your XAP file or absolute as defined by the UriKind
property.
As you saw earlier, your XML file is located in
the same directory as your XAP, so you can simply
pass in the XML file name directory without
qualifying with any path modifiers or folder names.
Obviously, this means that your UriKind property is
Relative, but for the sake of simplicity, I go with
RelativeOrAbsolute so that all types of Uri values
can be accepted without you having to consciously
replace it yourself each time you decide to change
the type of the path you specify.
Ok, in this page you got a good look at the code
that sets up and downloads your XML file. In the
next page, let's look at the XMLFileLoaded event
handler
Onwards to the
next
page!
|