0

我正在尝试将某些文本的内容序列化为 XML 文件(在用户保存他们的选择时执行),然后将其反序列化(当用户选择显示他们保存的选择时)。

我一直在关注以下关于序列化的教程。

我也尝试通过 LINQ to XML 执行此操作,但要么出现命名空间错误,要么该工具没有返回错误,但没有工作(与下面描述的相同问题)。

我遇到的问题是我的代码没有返回任何错误,但函数不起作用(我有一个标签控件,可以让我看到正在返回的“catch”)。我正在使用 C# 在 Expression Blend 中构建该工具。

这是我的 SaveSelection.cs 类

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; }
}
}

这是我的 C# 代码

// 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();
        }

我已将我先前尝试的证据之一注释掉,因此您可以查看我尝试过的先前格式。

我的问题是,当我尝试使用该工具时,SelectionLabel.Text 返回“保存您的选择时出现问题。请稍后再试。” 所以我知道代码正在返回捕获而不是执行“尝试”。

有什么帮助吗??

2012 年 6 月 18 日编辑:下面的代码是根据问题的正确答案工作的代码。

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.";
        }
    }
4

1 回答 1

0

看起来您的问题是因为您正在尝试文件,但该文件并非来自用户操作启动的 FileSaveDialog。您遇到了 Silverlight 的安全功能,不允许您访问本地文件系统。相反,请尝试写入 IsolatedStorage。但是,请注意最终用户可以完全(以及有选择地)禁用应用程序存储,因此您还需要处理这些异常。

这是一篇关于如何使用 IsolatedStorage 的快速文章。

于 2012-06-13T17:55:52.980 回答