1

XmlSerializer我使用and序列化/反序列化包含双精度值的对象StreamWriter。在生成的.xml文件中,我发现存储为十进制值的双精度值。我有没有办法以十六进制形式生成双精度而不是十进制?

一些代码。

序列化的类

[Serializable()]  
public class Variable
{        
    public int shift;
    public int size;
    public int min;
    public int max;
    public string name;
    public string del;

    public Variable()
    {
        shift = 26;
        size = 0;
        min = 0;
        max = 0;
        name = "noname";
        del = "-";
    }
}

序列化器类

/// <summary>
/// class to serialize/deserialize another class
/// </summary>
public class serializator
{
    private serializator()
    {
    }

    /// <summary>
    /// serializator
    /// </summary>
    /// <param name="fname">filename to serialize</param>
    /// <param name="z">object to serialize</param>
    static public void ser(string fname, object z)
    {
        System.Type st = z.GetType();
        XmlSerializer xSer = new XmlSerializer(st);
        StreamWriter sWri = new StreamWriter(fname);
        xSer.Serialize(sWri, z);
        sWri.Close();
    }

    /// <summary>
    /// deserializator
    /// usage: fooclass fc = (fooclass)serializator.dser("fname.xml", fc);
    /// </summary>
    /// <param name="fname">filename to dser</param>
    /// <param name="z">object to grab type</param>
    /// <returns></returns>
    static public object dser(string fname, object z)
    {
        if (fname != null && fname != "" && z != null)
        {
            try
            {
                object rez = new object();
                XmlSerializer xSer = new XmlSerializer(z.GetType());
                StreamReader sRea = new StreamReader(fname);
                rez = xSer.Deserialize(sRea);
                sRea.Close();
                return rez;
            }
            catch(Exception e)
            {
                System.Windows.Forms.MessageBox.Show("config ouch\r\n"+e.Message);
                return null;
            }
        }
        else
        {
            return null;
        }
    }
}

来自主程序的一段代码

Variable v = new Variable();
serializator.ser("foo.xml", v);  

生成的 xml 文件

<?xml version="1.0" encoding="utf-8"?>
  <Variable>
    <shift>26</shift>
    <size>0</size>
    <min>0</min>
    <max>0</max>
    <name>noname</name>
    <del>-</del>
  </Variable>  

在这种特殊情况下,我想看到<shift>1A</shift>or 。有可能吗?<shift>0x1A</shift><shift>26</shift>

4

1 回答 1

2

您可以使用“变量”类的代码而不是您的代码:

[Serializable()]
public class Variable
{
    int _shiftInt;

    public string shift
    {
        get
        {
            return _shiftInt.ToString("X");
        }
        set
        {
            _shiftInt = int.Parse(value, System.Globalization.NumberStyles.HexNumber);
        }
    }

    public int size { get; set; }
    public int min { get; set; }
    public int max { get; set; }
    public string name { get; set; }
    public string del { get; set; }

    public Variable()
    {
        _shiftInt = 26;
        size = 0;
        min = 0;
        max = 0;
        name = "noname";
        del = "-";
    }
}
于 2012-09-19T13:00:28.923 回答