G
Guest
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.
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();
}
}
}
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.
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();
}
}
}