C#XML可序列化错误

时间:2010-06-03 23:14:39

标签: c# xml-serialization

我正在尝试XML序列化一个类对象,但遇到以下问题:

代码编译正常,消息框显示所有正确的数据,但是当我查看XML时,它似乎不包含实际传输的数据,即XML中缺少FireGridLocation数据。

        XmlSerializer s;
        StringWriter w;

        FireGridUnit fireGridUnit = new FireGridUnit();
        fireGridUnit.FireGridLocation = new GridUnit(GridLock.getColumn, GridLock.getRow);

        MessageBox.Show("gridlock col " + GridLock.getColumn);


        MessageBox.Show("column fire " + fireGridUnit.FireGridLocation.getColumn);
        MessageBox.Show("row fire " + fireGridUnit.FireGridLocation.getRow);

        s = new XmlSerializer(typeof(FireGridUnit));
        w = new StringWriter();
        s.Serialize(w, fireGridUnit);

        MessageBox.Show(w.ToString());

alt text http://img52.imageshack.us/img52/220/errorce.jpg

这是FireGridUnit:

[Serializable]
public class FireGridUnit
{
    /// <summary>
    /// Location storage
    /// </summary>
    //public GridUnit FireGridLocation { get; set; }

    public GridUnit FireGridLocation;
}

这是GridUnit类:

public class GridUnit
{
    /// <summary>
    /// Default initialization
    /// </summary>
    public GridUnit()
    {
        Column = -1;
        Row = -1;
    }

    /// <summary>
    /// Initialize to supplied coordinate
    /// </summary>
    /// <param name="column"></param>
    /// <param name="row"></param>
    public GridUnit(int column, int row)
    {
        Column = column;
        Row = row;
    }

    /// <summary>
    /// Set/Return Column
    /// </summary>
    //public int Column { get; set; }

    private int Column;

    public int getColumn
    {
        get { return Column; }
    }

    /// <summary>
    /// Set/Return Row
    /// </summary>
    //public int Row { get; set; }

    private int Row;

    public int getRow
    {
        get { return Row; }
    }
}

如果您能协助解决此问题,我们非常欢迎您提供意见。

1 个答案:

答案 0 :(得分:0)

XML序列化只能序列化读/写属性。您的getColumngetRow属性是只读的,因此无法序列化。

BTW,XML序列化不需要Serializable属性