Serialization problem...

J

Jeti

I try, but i cant serialize Color field when serialiazing class that
contains it! How to do it?
Heres how i do it:

//Class to serialize
public class Colors

{

public Color Back;



public int LineWidth;

public void SetToDefault ()

{

Back = Color.FromArgb (255, 192, 192);

LineWidth = 1;

}



//I try to serialize it like this:

[STAThread]

static void Main()

{

Colors c = new Colors();

c.SetToDefault ();



XmlSerializer serializer = new XmlSerializer(typeof(Colors));

// Create an XmlTextWriter using a FileStream.

Stream fs = new FileStream("options.xml", FileMode.Create);

XmlWriter writer = new XmlTextWriter(fs, new UTF8Encoding());

// Serialize using the XmlTextWriter.

serializer.Serialize(writer, c);

writer.Close();



}

// END OF CODE.

when i look at the generated xml, only integer property serialized ok, but
color property was empty:
<?xml version="1.0" encoding="utf-8"?>

<Colors xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<Back />

<LineWidth>1</LineWidth>

</Colors>



Where do i go wrong?
please help!
 
?

=?ISO-8859-2?Q?Marcin_Grz=EAbski?=

Hi Jeti,
I try, but i cant serialize Color field when serialiazing class that
contains it! How to do it?
Heres how i do it:

//Class to serialize
public class Colors

{

public Color Back;



public int LineWidth;

public void SetToDefault ()

{

Back = Color.FromArgb (255, 192, 192);

LineWidth = 1;

}



//I try to serialize it like this:

[STAThread]

static void Main()

{

Colors c = new Colors();

c.SetToDefault ();



XmlSerializer serializer = new XmlSerializer(typeof(Colors));

// Create an XmlTextWriter using a FileStream.

Stream fs = new FileStream("options.xml", FileMode.Create);

XmlWriter writer = new XmlTextWriter(fs, new UTF8Encoding());

// Serialize using the XmlTextWriter.

serializer.Serialize(writer, c);

writer.Close();



}

// END OF CODE.

when i look at the generated xml, only integer property serialized ok, but
color property was empty:
<?xml version="1.0" encoding="utf-8"?>

<Colors xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<Back />

<LineWidth>1</LineWidth>

</Colors>

I don't have any practise with XML serializer, but...

If your color data has less or equal to 32bits then you can
serialize it as a simple Int32.

Color.ToArgb();
Color.FromArgb();

Regards

Marcin
 
J

Jeti

I don't have any practise with XML serializer, but...
If your color data has less or equal to 32bits then you can
serialize it as a simple Int32.

Color.ToArgb();
Color.FromArgb();


Thanx for so quick reply, but i gave up. :)
I did it using "BinaryFormatter"...

thanx anyway :)
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Marcin,

The problem, I believe, is that the XmlSerializer serializes only public
fields and properties. Even though the Color type is marked as serializable
it has no prublic mebers to serialize. That's why the best you can get is an
empty element in the xml file. So, my suggestion is to mark thet field as
non-xml-serializable and add another (unfortunately public) property just
for the purpose of serialization.

public class Colors
{

[XmlIgnore]
public Color Back;

/// <remarks/>
public int LineWidth;
public void SetToDefault ()

{

LineWidth = 1;
Back = Color.FromArgb (255, 192, 192);

}

//Used only for serialization purposes
[XmlElement("Back", typeof(int))]
public int BackColor
{
get
{
return Back.ToArgb();
}
set
{
Back = Color.FromArgb(value);
}
}

}

This should help

--
HTH
Stoitcho Goutsev (100) [C# MVP]


Marcin Grzêbski said:
Hi Jeti,
I try, but i cant serialize Color field when serialiazing class that
contains it! How to do it?
Heres how i do it:

//Class to serialize
public class Colors

{

public Color Back;



public int LineWidth;

public void SetToDefault ()

{

Back = Color.FromArgb (255, 192, 192);

LineWidth = 1;

}



//I try to serialize it like this:

[STAThread]

static void Main()

{

Colors c = new Colors();

c.SetToDefault ();



XmlSerializer serializer = new XmlSerializer(typeof(Colors));

// Create an XmlTextWriter using a FileStream.

Stream fs = new FileStream("options.xml", FileMode.Create);

XmlWriter writer = new XmlTextWriter(fs, new UTF8Encoding());

// Serialize using the XmlTextWriter.

serializer.Serialize(writer, c);

writer.Close();



}

// END OF CODE.

when i look at the generated xml, only integer property serialized ok, but
color property was empty:
<?xml version="1.0" encoding="utf-8"?>

<Colors xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<Back />

<LineWidth>1</LineWidth>

</Colors>

I don't have any practise with XML serializer, but...

If your color data has less or equal to 32bits then you can
serialize it as a simple Int32.

Color.ToArgb();
Color.FromArgb();

Regards

Marcin
 
J

Jeti

thanks. i just tried this and it works fine :)


The problem, I believe, is that the XmlSerializer serializes only public
fields and properties. Even though the Color type is marked as serializable
it has no prublic mebers to serialize. That's why the best you can get is an
empty element in the xml file. So, my suggestion is to mark thet field as
non-xml-serializable and add another (unfortunately public) property just
for the purpose of serialization.

public class Colors
{

[XmlIgnore]
public Color Back;

/// <remarks/>
public int LineWidth;
public void SetToDefault ()

{

LineWidth = 1;
Back = Color.FromArgb (255, 192, 192);

}

//Used only for serialization purposes
[XmlElement("Back", typeof(int))]
public int BackColor
{
get
{
return Back.ToArgb();
}
set
{
Back = Color.FromArgb(value);
}
}

}

This should help

--
HTH
Stoitcho Goutsev (100) [C# MVP]


Marcin Grzêbski said:
Hi Jeti,
I try, but i cant serialize Color field when serialiazing class that
contains it! How to do it?
Heres how i do it:

//Class to serialize
public class Colors

{

public Color Back;



public int LineWidth;

public void SetToDefault ()

{

Back = Color.FromArgb (255, 192, 192);

LineWidth = 1;

}



//I try to serialize it like this:

[STAThread]

static void Main()

{

Colors c = new Colors();

c.SetToDefault ();



XmlSerializer serializer = new XmlSerializer(typeof(Colors));

// Create an XmlTextWriter using a FileStream.

Stream fs = new FileStream("options.xml", FileMode.Create);

XmlWriter writer = new XmlTextWriter(fs, new UTF8Encoding());

// Serialize using the XmlTextWriter.

serializer.Serialize(writer, c);

writer.Close();



}

// END OF CODE.

when i look at the generated xml, only integer property serialized ok, but
color property was empty:
<?xml version="1.0" encoding="utf-8"?>

<Colors xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<Back />

<LineWidth>1</LineWidth>

</Colors>

I don't have any practise with XML serializer, but...

If your color data has less or equal to 32bits then you can
serialize it as a simple Int32.

Color.ToArgb();
Color.FromArgb();

Regards

Marcin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top