Tutorials Books Videos Forums

Change the theme! Search!
Rambo ftw!

Customize Theme


Color

Background


Done

Writing to an XML File

by kirupa   | filed under Silverlight, WPF, and Blend

This is an archived tutorial from the kirupa.com legacy collection. It covers software that may no longer be available, but it is kept online because the ideas still hold up.

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!

Code for Writing to an XML File

At a high level, there are three things that we need to do:

  1. Display a dialog that allows a user to pick a location to save the file.
  2. Create the XML data that will be saved in a file.
  3. 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;

Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.

Your support keeps this site going! 😇

Kirupa's signature!

The KIRUPA Newsletter

Thought provoking content that lives at the intersection of design 🎨, development 🤖, and business 💰 - delivered weekly to over a bazillion subscribers!

SUBSCRIBE NOW

Creating engaging and entertaining content for designers and developers since 1998.

Follow:

Popular

Loose Ends

:: Copyright KIRUPA 2026 //--