将对象序列化为xml问题

时间:2010-02-06 00:20:20

标签: c# xml xml-serialization

我无法将对象序列化为xml。

我有一个类序列很好:

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



}

以及序列化它的代码(Settings是上面Settings类的一个实例)

 //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文件。我究竟做错了什么? 谢谢!

2 个答案:

答案 0 :(得分:3)

您需要读/写属性。

事实上,来自MSDN:http://msdn.microsoft.com/en-us/library/bdxxw552.aspx

Serialize方法将对象的公共字段和读/写属性转换为XML。它不转换方法,索引器,私有字段或只读属性。要序列化所有对象的字段和属性(public和private),请使用BinaryFormatter。

答案 1 :(得分:0)

要使XML序列化起作用,您需要get和setter。你刚刚获得了SOX_PATH和SOX_PATH。

如果没有这些,它就无法从XML反序列化对象。