开发者

serialize object to xml problems

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 in开发者_运维问答stance 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!


You need read/write properties.

In fact, from MSDN: http://msdn.microsoft.com/en-us/library/bdxxw552.aspx

The Serialize method converts the public fields and read/write properties of an object into XML. It does not convert methods, indexers, private fields, or read-only properties. To serialize all of an object's fields and properties, both public and private, use the BinaryFormatter.


For XML serialization to work, you need both get and setters. You have just getters for SOX_PATH and SOX_PATH.

Without these it can't deserialize the objects back from XML.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜