I am having trouble serializing an object to xml.
I have one class that serializs fine:
public class GlobalInfo
{
public string Ripper = "";
public string Lineage = "";
}
I have the code that serializes it here (GlobalInfoData is an instance of GlobalInfo class above)
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(GlobalInfoData.GetType());
TextWriter w = new StreamWriter(Application.StartupPath + @"\GlobalInfo.xml");
x.Serialize(w, GlobalInfoData);
w.Close();
This works fine.
I then have another class:
public class Settings
{
public delegate void SettingsChangedHandler(object sender, EventArgs e);
public event SettingsChangedHandler SettingsChanged;
#region SoX
private string sox_path;
public string SOX_PATH { get { return sox_path; } }
private string sox_version;
public string SOX_VERSION { get { return sox_version; } }
public bool SetSOXPath(string soxpath)
{
string sox_result = ValidateSOX(soxpath);
if (sox_result != null)
{
sox_path = soxpath;
sox_version = sox_result;
return true;
}
else
{
sox_path = "";
sox_version = "";
return false;
}
}
public string ValidateSOX(string soxpath)
{
if (Path.GetFileName(soxpath).ToUpper() == "SOX.EXE")
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.CreateNoWindow = true;
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = soxpath;
proc.StartInfo.Arguments = "--version";
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.Start();
string output = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
if (output.Contains("sox: SoX v") == true)
{
int i = output.IndexOf("sox: SoX v");
string version = output.Substring(i + 10);
return version;
}
else
{
return null;
}
}
else
return null;
}
#endregion
#region LAME
private string lame_path;
public string LAME_PATH { get { return lame_path; } }
public bool SetLAMEPath(string lamepath)
{
if (ValidateLAME(lamepath) == true)
{
lame_path = lamepath;
return true;
}
else
{
lame_path = "";
return false;
}
}
public bool ValidateLAME(string lamepath)
{
if (Path.GetFileName(lamepath).ToUpper() == "LAME.EXE")
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.CreateNoWindow = true;
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = lamepath;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.UseShellExecute = false;
proc.Start();
string output = proc.StandardError.ReadLine();
proc.WaitForExit();
if (output.StartsWith("LAME") == true)
return true;
else
return false;
}
else
return false;
}
private string flac_path;
public string FLAC_PATH { get { return flac_path; } }
private string default_dir;
public string DEFAULT_DIR { get { return default_dir; } }
#endregion
#region FLAC
public bool SetFLACPath(string flacpath)
{
if (ValidateFLAC(flacpath) == true)
{
flac_path = flacpath;
return true;
}
else
{
flac_path = "";
return false;
}
}
public bool ValidateFLAC(string flacpath)
{
if (Path.GetFileName(flacpath).ToUpper() == "FLAC.EXE")
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.CreateNoWindow = true;
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = flacpath;
proc.StartInfo.Arguments = "-v";
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.Start();
string output = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
if (output.StartsWith("flac") == true)
return true;
else
return false;
}
else
return false;
}
public bool SaveSettings()
{
return true;
}
public bool LoadSettings()
{
return true;
}
#endregion
}
and the code to serialize it (Settings is an instance of Settings class above)
//Serialization
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(Settings.GetType());
TextWriter w = new StreamWriter(settingspath);
x.Serialize(w, Settings);
w.Close();
This just creates an xml file containing the xml header but no data. What am I doing wrong? Thanks!