Xml 序列化和编写器 [英] Xml Serializing and Writer

查看:34
本文介绍了Xml 序列化和编写器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

in one class I create with a button click a XML document:

    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 文件也已创建.

There I haven't problems. The folder is created and the xml file also.

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

So in an other class I want to serialize objects into the xml file 'config.xml' (The variable userConfigurePath is static in the classe mentionned above):

 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 文件中.为什么?

The result is that the object isn't saved in the config.xml file. Why?

编辑工具的类:

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

   public Tool() { }
}

第二次

我发现在创建 xml 文件时,我无法删除 manuel 这些文件夹(在应用程序关闭后).为什么?

I see that i can't delete manuel these folder (after the application is closed) with the xml file when it is created. Why?

第三次

我更改了代码:

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 就不会接受它.只序列化了第一个工具:

Now the first object is serialized. But if i create another tool in the same way the config.xml doesn't take it. Only the first tool is serialized:

<?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>

推荐答案

您需要关闭 StreamWriter 对象才能将数据刷新到文件中.请尝试以下操作:

You need to close the StreamWriter object to flush the data into the file. Please try the following:

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

这篇关于Xml 序列化和编写器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆