Click here to Skip to main content
15,881,755 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, I have following problem in my WPF app:

I want to save application data to disk. I am trying to do this in main window Closing event.

Code:
C#
XmlDocument Doc = new XmlDocument();
XmlElement root = Doc.CreateElement("fileset");

foreach(FileModel file in set)
{
    XmlElement fileElem = Doc.CreateElement("file");
    root.AppendChild(fileElem);

    fileElem.SetAttribute("path", file.Path);
}

System.IO.File.Create(path);

using (System.IO.StreamWriter file = new System.IO.StreamWriter(path, true))
{
    System.IO.StringWriter sw = new System.IO.StringWriter();
    XmlTextWriter tx = new XmlTextWriter(sw);
    Doc.WriteTo(tx);

    file.Write(tx.ToString());
}

// Doc.Save(path);


Problem is causing new System.IO.StreamWriter(path, true) (Doc.Save(path) as well), which never returns. Main window gets black and I have to kill app using taskmanager. System.IO.File.Create(path); works fine.

Any ideas?
Posted
Comments
ZurdoDev 2-Aug-13 9:16am    
If you have code that works fine why don't you use it? I guess I'm confused.
patrik polakovic 2-Aug-13 9:19am    
Creation of file "path" works fine. But its empty. Filling it with data is not working.
Sergey Alexandrovich Kryukov 2-Aug-13 10:21am    
It sounds as if you didn't use the debugger...
—SA

1 solution

Throw away the line System.IO.File.Create(path); - it's useless.
C#
XmlWriterSettings xmlSettings = new XmlWriterSettings();
xmlSettings.Encoding = System.Text.Encoding.UTF8;
xmlSettings.Indent = true;
xmlSettings.NewLineHandling = NewLineHandling.None;
xmlSettings.NewLineOnAttributes = false;
XmlWriter writer = XmlWriter.Create(path, xmlSettings);
writer.WriteStartDocument(true);
Doc.WriteTo(writer);
writer.WriteEndDocument();
writer.Close();
 
Share this answer
 
Comments
patrik polakovic 5-Aug-13 4:43am    
Hello Bernhard, I've already solved this issue, but thank you for your solution. You are right,
System.IO.File.Create(path); was really useless and causing my problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900