序列化对象到XML问题
问题描述:
我有麻烦序列化对象到XML。序列化对象到XML问题
我有一个类,serializs罚款:
public class GlobalInfo
{
public string Ripper = "";
public string Lineage = "";
}
我有一个序列化在这里(GlobalInfoData高于GlobalInfo类的一个实例)
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();
能正常工作的代码。
我然后有另一个类:
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
}
和序列化它的代码(设定是上述设置的类的实例)
//Serialization
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(Settings.GetType());
TextWriter w = new StreamWriter(settingspath);
x.Serialize(w, Settings);
w.Close();
这只是创建包含XML头的XML文件但没有数据。我究竟做错了什么? 谢谢!
答
您需要读取/写入属性。
事实上,从MSDN:http://msdn.microsoft.com/en-us/library/bdxxw552.aspx
serialize方法转换公共字段和读取的对象的/写属性为XML。它不会转换方法,索引器,专用字段或只读属性。要序列化公共和私有对象的所有字段和属性,请使用BinaryFormatter。
答
要使XML序列化起作用,您需要get和setters。你只有SOX_PATH和SOX_PATH的getters。
没有这些,它不能将对象从XML反序列化。
啊好吧是的我只想保存存储的值,所以我应该只为保存公共数据和导入/导出一个类? – Dave 2010-02-06 00:28:11
@Dave:是的,你可以做到这一点。你有你工作的*工作班;当你准备保存时,你可以实例化并设置公共属性并通过XmlSerializer运行。但是,如果您使用适当的公共获取/设置字段/属性创建* working *类,那并不是真的必要。在不需要XmlSerializer的公共字段上使用'[XmlIgnore]'。 – IAbstract 2010-02-06 00:41:51