|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Can't serialize a simple string when using the FileOpenDialogThe strings will contain paths to files that are chosen by a user via the OpenFileDialog. The serialization works perfectly if I set the path values via code such as: string sFilePath = @"C:\test.txt"; However, if the string originated from the OpenFileDialog the serialization will not work? There is something different about the string that comes from the OpenFileDialog??? The Form1_Closing code below works perfectly fine, oConfig is the serializable class with 3 fields. If, however, the first sFilePath is commented out and the call to OpenFileDialog is uncommented the string will not serialize. Farther below is my complete serializable class. I am using .net 2003 private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) { string sFilePath = ""; sFilePath = @"C:\Temp\test.txt"; // if(dlgOpenFile.ShowDialog() == DialogResult.OK) //{ // sFilePath=dlgOpenFile.FileName; // } Config oConfig = new Config(sFilePath,txtDataFile.Text,txtOutputFile.Text); oConfig.Save(); } //*** serializable class below *** using System; using System.Xml.Serialization; using System.IO; namespace FoGen { /// <summary> /// Summary description for Config. /// </summary> /// [ Serializable() ] public class Config { private const string _sFilePath = "config.xml"; private string _sXsltFile; private string _sXmlDataFile; private string _sOutputFile; public string XmlDataFile { get { return _sXmlDataFile; } set { _sXmlDataFile = value; } } public string OutputFile { get { return _sOutputFile; } set { _sOutputFile = value; } } public string XsltFile { get { return _sXsltFile; } set { _sXsltFile = value; } } public Config() { } public Config(string sXsltFile, string sXmlDataFile, string sOutputFile) { _sXsltFile=sXsltFile; _sXmlDataFile=sXmlDataFile; _sOutputFile=sOutputFile; } public bool Initialize() { if (File.Exists(_sFilePath)) { XmlSerializer ser = new XmlSerializer(typeof(Config)); FileStream fs = new FileStream(_sFilePath, FileMode.Open); Config oConfig = (Config)ser.Deserialize(fs); _sXsltFile=oConfig.XsltFile; _sXmlDataFile=oConfig.XmlDataFile; _sOutputFile=oConfig.OutputFile; fs.Close(); return true; } else return false; } public void Save() { XmlSerializer ser = new XmlSerializer(typeof(Config)); TextWriter tw = new StreamWriter(_sFilePath, false); Config oConfig = new Config(); oConfig.XsltFile = this.XsltFile; oConfig.XmlDataFile = this.XmlDataFile; oConfig.OutputFile = this.OutputFile; ser.Serialize(tw, oConfig); tw.Close(); } } } -- Jerry J Jerry J <Jer***@discussions.microsoft.com> wrote:
> I have a simple serializable class that just has 3 public string properties. "Will not work" and "will not serialize" don't help to pin it down > The strings will contain paths to files that are chosen by a user via the > OpenFileDialog. > > The serialization works perfectly if I set the path values via code such as: > string sFilePath = @"C:\test.txt"; However, if the string originated from > the OpenFileDialog the serialization will not work? There is something > different about the string that comes from the OpenFileDialog??? The > Form1_Closing code below works perfectly fine, oConfig is the serializable > class with 3 fields. If, however, the first sFilePath is commented out and > the call to OpenFileDialog is uncommented the string will not serialize. > Farther below is my complete serializable class. much. What actually happens? Could you post a short but complete program which demonstrates the problem? See http://www.pobox.com/~skeet/csharp/complete.html for details of what I mean by that. -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too Nothing gets written to disk at all when using the open file dialog.
To run this code, you just need a single form with a button, paste the config class into a config.cs file. In your button_onclick code, call config cf = new config("TestText1","TestText2","TestText3"). config.save(); When you execute the above you will see the three strings saved in config.xml in the bin folder of your application. If you then replace the one of the strings with a string that was received via the file open dialog, you will see config.xml will not contain the new strings. Nothing gets written to disk? That is at least what is happening to me. I appreciate your help. -- Show quoteJerry J "Jon Skeet [C# MVP]" wrote: > Jerry J <Jer***@discussions.microsoft.com> wrote: > > I have a simple serializable class that just has 3 public string properties. > > The strings will contain paths to files that are chosen by a user via the > > OpenFileDialog. > > > > The serialization works perfectly if I set the path values via code such as: > > string sFilePath = @"C:\test.txt"; However, if the string originated from > > the OpenFileDialog the serialization will not work? There is something > > different about the string that comes from the OpenFileDialog??? The > > Form1_Closing code below works perfectly fine, oConfig is the serializable > > class with 3 fields. If, however, the first sFilePath is commented out and > > the call to OpenFileDialog is uncommented the string will not serialize. > > Farther below is my complete serializable class. > > "Will not work" and "will not serialize" don't help to pin it down > much. What actually happens? > > Could you post a short but complete program which demonstrates the > problem? > > See http://www.pobox.com/~skeet/csharp/complete.html for details of > what I mean by that. > > -- > Jon Skeet - <sk***@pobox.com> > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet > If replying to the group, please do not mail me too > Jerry J <Jer***@discussions.microsoft.com> wrote:
> Nothing gets written to disk at all when using the open file dialog. And how far does the code get when you're running it in the debugger, stepping through carefully? > To run this code, you just need a single form with a button, paste the No, I need you to post a short but complete program. Please read the > config class into a config.cs file. In your button_onclick code, call config > cf = new config("TestText1","TestText2","TestText3"). > config.save(); link I posted before. -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too /*
Here is a simple Form1 with a simplified serializable class. All self contained. Can't be a console because open file dialog is needed. Run as is at first. config.xml wil be created in \bin\debug folder. Then uncomment the code that uses the open file dialog. Use the dialog to chose a file. When complete config.xml will not be updated. */ using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Xml.Serialization; using System.IO; namespace TestSerial { public class Form1 : System.Windows.Forms.Form { private System.ComponentModel.Container components = null; public Form1() { InitializeComponent(); Config cf = new Config(); cf.StringToSerialize = @"c:\test.txt"; // run once as is. config.xml will be in // bin/debug folder as expected. // Now, uncomment below, ofd.FileName will // not be saved because config.xml will // not be rewritten. /* OpenFileDialog ofd = new OpenFileDialog(); if(ofd.ShowDialog() == DialogResult.OK) { cf.StringToSerialize=ofd.FileName; } */ cf.Save(); } private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.Size = new System.Drawing.Size(300,300); this.Text = "Form1"; } [STAThread] static void Main() { Application.Run(new Form1()); } } [ Serializable() ] public class Config { private const string _sFilePath = "config.xml"; private string _sStringToSerialize; public string StringToSerialize { get { return _sStringToSerialize; } set { _sStringToSerialize = value; } } public Config() { } public void Save() { XmlSerializer ser = new XmlSerializer(this.GetType()); TextWriter tw = new StreamWriter(_sFilePath, false); ser.Serialize(tw, this); tw.Close(); } } } Jerry J <Jer***@discussions.microsoft.com> wrote:
> /* Yes it will - but in the directory you've specified in the open file > Here is a simple Form1 with a simplified serializable class. All self > contained. Can't be a console because open file dialog is needed. Run as is > at first. config.xml wil be created in \bin\debug folder. > Then uncomment the code that uses the open file dialog. Use the dialog to > chose a file. When complete config.xml will not be updated. > */ dialog. Your serialization code saves to wherever the current working directory of the process is. Using the Open File dialog changes that current directory. -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too Wow! I wasted so much time on that!! Thank you!
-- Show quoteJerry J "Jon Skeet [C# MVP]" wrote: > Jerry J <Jer***@discussions.microsoft.com> wrote: > > /* > > Here is a simple Form1 with a simplified serializable class. All self > > contained. Can't be a console because open file dialog is needed. Run as is > > at first. config.xml wil be created in \bin\debug folder. > > Then uncomment the code that uses the open file dialog. Use the dialog to > > chose a file. When complete config.xml will not be updated. > > */ > > Yes it will - but in the directory you've specified in the open file > dialog. Your serialization code saves to wherever the current working > directory of the process is. Using the Open File dialog changes that > current directory. > > -- > Jon Skeet - <sk***@pobox.com> > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet > If replying to the group, please do not mail me too > |
|||||||||||||||||||||||