Xml序列化和编写器

时间:2014-02-11 13:54:27

标签: c# xml streamwriter

在我使用按钮创建的一个类中单击XML文档:

    private void buttonCreate_Click(object sender, RoutedEventArgs e)
    {
        DialogResult result = folderElg.ShowDialog();

        if (result == System.Windows.Forms.DialogResult.OK)
        {
            textBoxPath.Text = folderElg.SelectedPath;
            userConfigurePath = folderElg.SelectedPath;
        }
        XmlDocument toolConfig = new XmlDocument();
        XmlNode myRoot;                               
        myRoot = toolConfig.CreateElement("Tool");    
        toolConfig.AppendChild(myRoot);
        toolConfig.Save(@userConfigurePath + "\\config.xml");}

我没有问题。该文件夹已创建,xml文件也已创建。

所以在另一个类中我想将对象序列化为xml文件'config.xml'(变量userConfigurePath在上面提到的classe中是静态的):

 public partial class MainWindow : Window
 {
    private string inputNewTool = "";
    private OpenFileDialog dlg = new OpenFileDialog();      

    public MainWindow()
    {
        InitializeComponent();
    }

    private void buttonAdd_Click(object sender, RoutedEventArgs e)
    {
        InputDialog input = new InputDialog();
        input.ShowDialog();
        inputNewTool = input.enteredTxt;

        if (inputNewTool != null)
        {
            System.Windows.Forms.MessageBox.Show("Chose the Tool's directory");
            dlg.DefaultExt = ".txt";
            dlg.Filter = "Text documents (.txt)|*.txt";

            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Tool tool = new Tool();
                tool.Name = inputNewTool;
                tool.Path = dlg.FileName;
                XmlSerializer serializer = new XmlSerializer(tool.GetType());
                StreamWriter writer = new StreamWriter(@Start.userConfigurePath + 
                "\\config.xml");
                serializer.Serialize(writer.BaseStream, tool);
            }

        }

结果是该对象未保存在config.xml文件中。为什么呢?

编辑工具类:

 public class Tool
{
    public   string Name { get; set; }
    public   string Path { get; set; }

   public Tool() { }
}

第二次编辑:

我看到我无法在创建xml文件时删除manuel这些文件夹(在应用程序关闭后)。为什么呢?

第三次编辑:

我改变了我的代码:

if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Tool tool = new Tool();
                tool.Name = inputNewTool;
                tool.Path = dlg.FileName;
                XmlSerializer serializer = new XmlSerializer(tool.GetType());
                using (var writer = new StreamWriter(@Start.userConfigurePath + 
                "\\config.xml"))
                {
                    serializer.Serialize(writer.BaseStream, tool);
                    writer.Close();
                }

现在第一个对象被序列化了。但是如果我以相同的方式创建另一个工具,则config.xml不会接受它。只有第一个工具被序列化:

<?xml version="1.0"?>
<Tool xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>dffss</Name>
<Path>D:\Users\xxxx\Documents\schulewochenbericht.txt</Path>
</Tool>

2 个答案:

答案 0 :(得分:4)

您需要关闭StreamWriter对象以将数据刷新到文件中。请尝试以下方法:

            XmlSerializer serializer = new XmlSerializer(tool.GetType());
            using (var writer = new StreamWriter(@Start.userConfigurePath + "\\config.xml"))
            {
                serializer.Serialize(writer.BaseStream, tool);
            }

答案 1 :(得分:0)

除了wdosanjos的答案之外,你还应该意识到你不能只编写Tool个对象的序列。如果要编写多个Tool,则需要编写Tool的数组或列表。