问题
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");}
There I haven't problems. The folder is created and the xml file also.
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);
}
}
The result is that the object isn't saved in the config.xml file. Why?
Edit the Tool's Class:
public class Tool
{
public string Name { get; set; }
public string Path { get; set; }
public Tool() { }
}
Second Edit:
I see that i can't delete manuel these folder (after the application is closed) with the xml file when it is created. Why?
Third Edit:
I changed my Code so:
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();
}
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>
回答1:
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);
}
回答2:
In addition to the answer from wdosanjos, you should also realize that you can't just write a sequence of Tool
objects. If you want to write more than one Tool
, then you need to write an array or list of Tool
.
来源:https://stackoverflow.com/questions/21703811/xml-serializing-and-writer