开发者

Serializing instances of the Unit class

I am trying to serialize this class:

[Serializable()]
public class RadPaneSetting
{
    public string ID;
    public string ParentID;
    public Unit Height;
    public Unit Width;
    public bool Collapsed;
    public PanePositions PanePosition;
}

Everything writes fine except for Height/Width. Right before serialization I am able to see Height开发者_C百科 as Unit(400, UnitType.Pixel), but then it does not write this to my XML file.

My XML:

<Value>
  <RadPaneSetting xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <ID>RadPane_5c2c5136a0afaa4d65aa0adae5926751adf2</ID>
    <ParentID>RadDockSplitter_bc10266da61e0a4cb9a9814a0e94357d3d50</ParentID>
    <Height />
    <Width />
    <Collapsed>false</Collapsed>
    <PanePosition>Top</PanePosition>
  </RadPaneSetting>
</Value>

Is this a known issue with the 'Unit' type? I couldn't find anything on Google and I wouldn't expect this to be an issue, but I am unable to think of any underlying causes, either.

EDIT: RadPaneSetting is stored in a dictionary. Dictionary isn't serializable by default, but I implemented a serializable dictionary. Code may be found here: http://www.dacris.com/blog/2010/07/31/CSerializableDictionaryAWorkingExample.aspx

private void WriteDataToPath(string sessionName)
{
    string dataPath = string.Format(@"C:\{0}.txt", sessionName);

    var data = Session[sessionName];

    if (data != null)
    {
        if (!File.Exists(dataPath))
        {
            File.Create(dataPath).Close();
        }

        FileStream writer = File.OpenWrite(dataPath);

        XmlSerializer serializer = new XmlSerializer(data.GetType());
        serializer.Serialize(writer, data);

        writer.Close();
    }
}


You'll need to serialize that type on your own. Something like this:

[XmlIgnore]
public Unit Height;

[XmlElement("Unit")]
public string HeightString
{
    get {return Height.ToString();}
    set {Height = Unit.Parse(value);}
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜