开发者

C# XML Serializable Error

i am trying to XML serialize a class object but having the following problem:

the code compiles fine and the messagebox displays all the correct data but when i view the XML it does not seem to include the data for the actual transfer i.e. FireGridLocation data is missing from the XML.

        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

here is the FireGridUnit:

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

    public GridUnit FireGridLocation;
}

and here is the GridUnit class:

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 GridU开发者_开发知识库nit(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; }
    }
}

if you can assist with this issue, your input is very welcome.


XML serialization can only serialize read/write properties. Your getColumn and getRow properties are read-only, so they can't be serialized.

BTW, the Serializable attribute is not necessary for XML serialization

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜