Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8075071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:52:10+00:00 2026-06-05T14:52:10+00:00

I am trying to Serialize the content of some text into an XML file

  • 0

I am trying to Serialize the content of some text into an XML file (performed when a user saves their selections), and then will later deserialize it (when the user chooses to display their saved selection).

I have been following the following tutorial on serialization.

I have also tried to do this via LINQ to XML but was either getting namespace errors, or the tool returned no errors, but did not work (with the same problem as described below).

The problem I am having is that my code is not returning any errors, but the function is not working (I have a label control that allows me to see that the ‘catch’ is being returned). I am building the tool in Expression Blend, using C#.

Here is my SaveSelection.cs Class

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml.Serialization;
using System.Xml;

namespace DYH
{
public class SaveSelections
{
    [XmlAttribute("Title")]
    public string Title
    { get; set; }

    [XmlElement("Roof")]
    public string RoofSelection
    { get; set; }

    [XmlElement("Cladding")]
    public string CladdingSelection
    { get; set; }

    [XmlElement("MixedCladding")]
    public string MixedCladdingSelection
    { get; set; }

    [XmlElement("FAJ")]
    public string FAJSelection
    { get; set; }

    [XmlElement("GarageDoor")]
    public string GarageDoorSelection
    { get; set; }

    [XmlElement("FrontDoor")]
    public string FrontDoorSelection
    { get; set; }
}
}

Here is my C# code

// Save Selection Button
        private void Button_SaveSelection_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {   
            try
            {
                // Save selections into the SavedSelections.xml doc
                SaveSelections userselection = new SaveSelections();
                userselection.Title = TextBox_SaveSelection.Text;
                userselection.RoofSelection = Button_Roof_Select.Text;
                userselection.CladdingSelection = Button_Cladding_Select.Text;
                userselection.MixedCladdingSelection = Button_MixedCladding_Select.Text;
                userselection.FAJSelection = Button_FAJ_Select.Text;
                userselection.GarageDoorSelection = Button_GarageDoor_Select.Text;
                userselection.FrontDoorSelection = Button_FrontDoor_Select.Text;

                SerializeToXML(userselection);

//              XDocument xmlSaveSelections = XDocument.Load("../SavedSelections.xml");
//          
//              XElement newSelection = new XElement("Selection", //xmlSaveSelections.Element("Selections").Add(
//                      //new XElement("Selection",
//                      new XElement("Title", TextBox_SaveSelection.Text),
//                      new XElement("RoofSelection", Button_Roof_Select.Text),
//                      new XElement("CladdingSelection", Button_Cladding_Select.Text),
//                      new XElement("MixedCladdingSelection", Button_MixedCladding_Select.Text),
//                      new XElement("FAJSelection", Button_FAJ_Select.Text),
//                      new XElement("GarageDoorSelection", Button_GarageDoor_Select.Text),
//                      new XElement("FrontDoorSelection", Button_FrontDoor_Select.Text));
//              
////                xmlSaveSelections.Add(newSelection);
////                xmlSaveSelections.Save("../SavedSelections.xml");

                SelectionLabel.Text = "Your selection has been saved as " + "'" + TextBox_SaveSelection.Text + "'. We suggest you write down the name of your selection.";
            }
            catch(Exception ex)
            {
                            throw ex;
                SelectionLabel.Text = "There was a problem saving your selection. Please try again shortly.";
            }
        }

        // Saves SaveSelection.cs to XML file SavedSelections.xml
        static public void SerializeToXML(SaveSelections selection)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(SaveSelections));
            TextWriter textWriter = new StreamWriter(@"/SavedSelections.xml");
            serializer.Serialize(textWriter, selection);
            textWriter.Close();
        }

I have left evidence of one of my previous attempts commented out so you can see a previous format I tried.

My issue is that when I try to use the tool, the SelectionLabel.Text returns “There was a problem saving your selection. Please try again shortly.” so I know that the code is returning the catch and not executing the ‘try’.

Any help??

Edit 18/6/2012: The below code was the code that worked as per correct answer to question.

public void Button_SaveSelection_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        string roofSelection = TextBox_SaveSelection.Text + "_RoofSelection";
        string claddingSelection = TextBox_SaveSelection.Text + "_CladdingSelection";
        string mixedCladdingSelection = TextBox_SaveSelection.Text + "_MixedCladdingSelection";
        string fajSelection = TextBox_SaveSelection.Text + "_FAJSelection";
        string garageDoorSelection = TextBox_SaveSelection.Text + "_GarageDoorSelection";
        string frontDoorSelection = TextBox_SaveSelection.Text + "_FrontDoorSelection";

        try
        {
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                // Gives us 6Mb of storage space in IsoStore
                Int64 isoSpaceNeeded = 1048576 * 6;
                Int64 currentIsoSpace = store.AvailableFreeSpace;

                // If space needed is greater than (>) space available, increase space
                if (isoSpaceNeeded > currentIsoSpace)
                {
                    // If user accepts space increase
                    if (store.IncreaseQuotaTo(currentIsoSpace + isoSpaceNeeded))
                    {
                        IsolatedStorageFileStream file = store.CreateFile("SavedSelections.txt");
                        file.Close();

                        // Stream writer to populate information in
                        using (StreamWriter sw = new StreamWriter(store.OpenFile("SavedSelections.txt", FileMode.Open, FileAccess.Write)))
                        {
                            appSettings.Add(roofSelection, Button_Roof_Select.Text);
                            sw.WriteLine(roofSelection);
                            appSettings.Add(claddingSelection, Button_Cladding_Select.Text);
                            sw.WriteLine(claddingSelection);
                            appSettings.Add(mixedCladdingSelection, Button_MixedCladding_Select.Text);
                            sw.WriteLine(mixedCladdingSelection);
                            appSettings.Add(fajSelection, Button_FAJ_Select.Text);
                            sw.WriteLine(fajSelection);
                            appSettings.Add(garageDoorSelection, Button_GarageDoor_Select.Text);
                            sw.WriteLine(garageDoorSelection);
                            appSettings.Add(frontDoorSelection, Button_FrontDoor_Select.Text);
                            sw.WriteLine(frontDoorSelection);
                        }

                        SelectionLabel.Text = "Your selection has been saved as " + "'" + TextBox_SaveSelection.Text + "'. We suggest you write down the name of your selection.";
                    }
                }
            }

            SelectionLabel.Text = "Your selection has been saved as " + "'" + TextBox_SaveSelection.Text + "'. We suggest you write down the name of your selection.";
        }
        catch //(Exception ex)
        {
            //throw ex;
            SelectionLabel.Text = "There was a problem saving your selection. Please try again shortly.";
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:52:13+00:00Added an answer on June 5, 2026 at 2:52 pm

    It looks like your issue is because you’re trying to a file, but that file did not come from a FileSaveDialog initiated by a user action. You’re running into a security feature of Silverlight where you’re not allowed access to the local file system. Instead, try writing to IsolatedStorage. However, be aware that end users can completely (as well as selectively) disable application storage so you’ll need to handle those exceptions as well.

    Here’s a quick article on how to use IsolatedStorage.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to add some contacts from an xml file which have been serialize
I'm trying to serialize some data to xml in a way that can be
I'm trying to serialize an object to XML that has a number of properties,
I'm trying to serialize some objects obtained from a 3rd Party .NET Lib to
i'm trying to serialize DataTable to Json or XML. is it possibly and how?
I've been trying to serialize an XML (jQuery object) to string to POST it
I am trying to open a file (included in my project as Content and
I'm trying to serialize some contents inside of a form: <form> <input ...> <input
I'm new in json and got some problems trying to serialize my objects with
I'm trying to serialize an object as XML and have been using a little

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.