by
kirupa | 26 April 2010
Silvelight has always had a way for applications
to persist some data inside an internal storage
mechanism called Isolated Storage. That was useful,
but it had some limitations such as lacking any
portability.
One of the features recently added to Silverlight
is the ability to allow a user to save application
data to a location on disk. For an example of
this, type in some values in the text fields below
and hit the save data button.
When you click on the Save Data button, a dialog
will appear asking you to save a file to disk:
Go ahead and save the file somewhere. Once it has
been saved, open the saved file Notepad or some
other editor:
Notice that your saved data is actually XML. By
the end of this tutorial, you will learn how to take
some data, process it as XML, and write the XML to
disk.
In my example, I have some UI that is basically
two textboxes and a button. Clicking the button
invokes the code that reads the values from the two
textboxes and puts them in an XML format for saving.
I am not going to describe the UI in any great
detail because it is fairly straightforward to
create, so I am going to focus on the more
interesting part - the code!
At a high level, there are three
things that we need to do:
- Display a dialog that allows a user to pick
a location to save the file.
- Create the XML data that will be saved in a
file.
- Write the file.
The above three items translated into code looks
as follows:
- private
void
SaveFile(object
sender,
RoutedEventArgs
e)
- {
- SaveFileDialog
saveFileDialog
=
new
SaveFileDialog();
-
- saveFileDialog.DefaultExt
=
"xml";
- saveFileDialog.Filter
=
"XML Files
(*.xml)|*.xml|All files (*.*)|*.*";
- saveFileDialog.FilterIndex
=
1;
-
- if
(saveFileDialog.ShowDialog()
==
true)
- {
- using
(Stream
stream
=
saveFileDialog.OpenFile())
- {
-
StreamWriter
sw
=
new
StreamWriter(stream,
System.Text.Encoding.UTF8);
- sw.Write(GetGeneratedXML().ToString());
- sw.Close();
-
- stream.Close();
- }
- }
- }
-
- private
XElement
GetGeneratedXML()
- {
- XElement
userInformation
=
new
XElement("names");
- userInformation.Add(new
XElement("first",
firstNameText.Text));
- userInformation.Add(new
XElement("last",
lastNameText.Text));
-
- return
userInformation;
- }
Let's look at each section of code in greater
detail starting with the SaveFile method (more
specifically, an event handler) that acts as the
brains behind the whole operation:
- private
void
SaveFile(object
sender,
RoutedEventArgs
e)
- {
- SaveFileDialog
saveFileDialog
=
new
SaveFileDialog();
-
- saveFileDialog.DefaultExt
=
"xml";
- saveFileDialog.Filter
=
"XML Files
(*.xml)|*.xml|All files (*.*)|*.*";
- saveFileDialog.FilterIndex
=
1;
-
- if
(saveFileDialog.ShowDialog()
==
true)
- {
- using
(Stream
stream
=
saveFileDialog.OpenFile())
- {
-
StreamWriter
sw
=
new
StreamWriter(stream,
System.Text.Encoding.UTF8);
- sw.Write(GetGeneratedXML().ToString());
- sw.Close();
-
- stream.Close();
- }
- }
- }
The first four lines are responsible for
displaying the Save File dialog and customizing what
it exposes to a user:
- SaveFileDialog
saveFileDialog
=
new
SaveFileDialog();
-
- saveFileDialog.DefaultExt
=
"xml";
- saveFileDialog.Filter
=
"XML Files
(*.xml)|*.xml|All files (*.*)|*.*";
- saveFileDialog.FilterIndex
=
1;
sd
|