Saving Files Locally in Silverlight - Page 4
       by kirupa  |  31 January 2010

In the previous page, we started looking at the code to see how it all fits together. We are almost done, so let's just finish up the last remaining lines and call it a day!

Opening a Pathway to the File
Right now, we are at the point where the Save As dialog has just been closed and an empty file has been created with the name and extension the user specified. This empty file needs to store the data that the user has entered, and that is where the rest of the code comes into play:

using (Stream stream = saveFileDialog.OpenFile())
{
StreamWriter sw = new StreamWriter(stream, System.Text.Encoding.UTF8);
sw.Write(GetGeneratedXML().ToString());
sw.Close();
 
stream.Close();
}

The first thing I do is use the OpenFile() method to open the file that was just created. I also declare a Stream object through which I can funnel in all kinds of things I want to store into the file.

Think of the stream object as a giant gate attached to a pathway through which you can send things through. All this gate can do is open or close, and by calling OpenFile, we have the gate wide open.

Writing our Data
With direct access to our file, the actual data you want to write to it are handled via the StreamWriter object called sw:

StreamWriter sw = new StreamWriter(stream, System.Text.Encoding.UTF8);

The StreamWriter class is optimized for sending in text/character based data, and since XML is just text, it is a great choice to use when a stream is involved. The first argument the StreamWriter constructor takes is a reference to the stream object we created earlier, and it also takes the encoding as its second argument.

Declaring and initializing our StreamWriter object sets us up nicely for being able to write data, and writing is handled by the Write method:

sw.Write(GetGeneratedXML().ToString());

The Write method is beautiful because its main argument is all of the data that you want written. It doesn’t matter how large or small the data is, for the StreamWriter handles breaking things up as necessary and making sure your app remains performant.

Closing the Pathway
The last thing that this block of code does is close the pathways that were opened by the Stream and StreamWriter objects:

sw.Close();
 
stream.Close();

They are both sort of self explanatory. The Close method commits any changes made to the file and closes the pathway.


Building the XML Data
There was one line that I kind of rushed through in my explanation:

sw.Write(GetGeneratedXML().ToString());

The Write method takes a function called GetGeneratedXML() whose output is converted to a string as its argument. The GetGeneratedXML method is where our XML data gets generated:

private XElement GetGeneratedXML()
{
XElement userInformation = new XElement("names");
userInformation.Add(new XElement("first", firstNameText.Text));
userInformation.Add(new XElement("last", lastNameText.Text));
 
return userInformation;
}

I am not going to delve into the details of LINQ in this tutorial, but to be very brief, notice that we are building up our XML tree by creating a root node called names and adding two children called first and last values are what the firstNameText and lastNameText textboxes you defined in XAML earlier contain.

The XML data once created is returned to whatever called it, and that is the Write method. Once you call ToString on the XML data, you now have a text-based representation of what needs to be written to the XML file the user created.

Conclusion
Hopefully this tutorial gave you a full end-to-end look at a small application that takes some user data and gives you the option of saving the data locally to disk. As always, below is the source code in case you want to dissect my example:

Download Source Files

If you noticed and/or are curious, the strange name stored in the XML file in my screenshts, it is Uther Lightbringer - one of the most awesome paladins from the WarCraft games.



1 | 2 | 3 | 4




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.