XML serializition.
-
Hello all, i'm making a dynamic light system, that contains scene's. A scene has a lamp (philips livingColor), min 1 max 8, and each lamp has its own platform. The save/edit a scene i am saving all data in the class Lamp (and motor) in a list . The struct: Color_point has string, int and a Color. However, if i want to serialise the class, i wont get a color in the xml. I know this is an private attribute, so i wont get something back. And I already read some more about making a different struc tfor the color. But.. how? See Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Collections;
using System.Collections.Specialized;
using System.Xml.Serialization;namespace Light
{
[Serializable()]
public struct Color_Point
{
public string Name;
public int Time;
public Color Rgb;public Color\_Point(string name, int time, Color rgb) { Name = name; Time = time; //in ms Rgb = rgb; } } \[Serializable()\] public class Lamp { public int address; public Color current\_Color; public Color previous\_Color; public List<Color\_Point> list = new List<Color\_Point>(); public Lamp() { address = 0; } public void setColor(Int32 time, Color rgb, string name) { int correct\_time = 0; if (name != "Fade") { if (list.Count == 0) list.Add(new Color\_Point(name, 0, rgb)); else { correct\_time = getTime("btnColor" + LastNumber()) + time; list.Add(new Color\_Point(name, correct\_time, rgb)); } } else list.Add(new Color\_Point(name, time, rgb)); }
How can i make sure that i also save the Colors?? Thank you!
-
Hello all, i'm making a dynamic light system, that contains scene's. A scene has a lamp (philips livingColor), min 1 max 8, and each lamp has its own platform. The save/edit a scene i am saving all data in the class Lamp (and motor) in a list . The struct: Color_point has string, int and a Color. However, if i want to serialise the class, i wont get a color in the xml. I know this is an private attribute, so i wont get something back. And I already read some more about making a different struc tfor the color. But.. how? See Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Collections;
using System.Collections.Specialized;
using System.Xml.Serialization;namespace Light
{
[Serializable()]
public struct Color_Point
{
public string Name;
public int Time;
public Color Rgb;public Color\_Point(string name, int time, Color rgb) { Name = name; Time = time; //in ms Rgb = rgb; } } \[Serializable()\] public class Lamp { public int address; public Color current\_Color; public Color previous\_Color; public List<Color\_Point> list = new List<Color\_Point>(); public Lamp() { address = 0; } public void setColor(Int32 time, Color rgb, string name) { int correct\_time = 0; if (name != "Fade") { if (list.Count == 0) list.Add(new Color\_Point(name, 0, rgb)); else { correct\_time = getTime("btnColor" + LastNumber()) + time; list.Add(new Color\_Point(name, correct\_time, rgb)); } } else list.Add(new Color\_Point(name, time, rgb)); }
How can i make sure that i also save the Colors?? Thank you!
An easy way around that is to make the color private and expose it's constituant parts (R, G, B) as pubic properties. A leaner way to do it would be to implement your own custom serializer for your class. See this[^] for a quick explanation and sample.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008