views:

87

answers:

1

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

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 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; }
    }
}

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

A: 

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

Thomas Levesque
yes im new at this but tracked the problem down to what i reported. this is useful good to know, knowledge. thank you.
iEisenhower