473,395 Members | 1,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

how to serialize font

I have class

FormData {
public System.Drawing.Font TextBoxFont;
}

I need to read/store this class in isolated storage in Winforms application.

Using

XmlSerializer formatter = new XmlSerializer(typeof(FormData),
"MyNameSpace");

causes exception

System.Drawing.Font cannot be serialized because it does not have a
parameterless constructor.

How to fix ?

Andrus.

Aug 5 '08 #1
8 5648
On Aug 5, 3:50*pm, "Andrus" <kobrule...@hot.eewrote:
I have class

FormData {
* *public System.Drawing.Font *TextBoxFont;

}

I need to read/store this class in isolated storage in Winforms application.

Using

XmlSerializer formatter = new XmlSerializer(typeof(FormData),
"MyNameSpace");

causes exception

System.Drawing.Font cannot be serialized because it does not have a
parameterless constructor.

How to fix ?
Avoid using XmlSerializer. It's not designed to work with
arbitrary .NET classes - you pretty much to have to write classes
specifically for it. In this case, either seialize Font by hand
directly via XmlReader/XmlWriter (preferrably) or XmlDocument; or,
write your own XmlSerializer-compatible class with the same properties
as Font (or rather, those properties that are sufficient to recreate
the font), and serialize that.
Aug 5 '08 #2
On Aug 5, 7:50 am, "Andrus" <kobrule...@hot.eewrote:
I have class

FormData {
public System.Drawing.Font TextBoxFont;

}

I need to read/store this class in isolated storage in Winforms application.

Using

XmlSerializer formatter = new XmlSerializer(typeof(FormData),
"MyNameSpace");

causes exception

System.Drawing.Font cannot be serialized because it does not have a
parameterless constructor.

How to fix ?

Andrus.
Must probably you need to cast the Font to ISerializable
Aug 5 '08 #3
"Andrus" <ko********@hot.eewrote:
System.Drawing.Font cannot be serialized because it does not have a
parameterless constructor.
Mark the Font member with the [XmlIgnore] attribute, and serialise some
simpler variables that will let you reconstruct the font (such as its name
and size).

Eq.
Aug 5 '08 #4
On Aug 5, 6:54*pm, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
Must probably you need to cast the Font to ISerializable
ISerializable has absolutely nothing to do with XmlSerializer - it is
used for binary and SOAP serialization only.
Aug 5 '08 #5
>Avoid using XmlSerializer. It's not designed to work with
>arbitrary .NET classes - you pretty much to have to write classes
<specifically for it. In this case, either seialize Font by hand
<directly via XmlReader/XmlWriter (preferrably) or XmlDocument; or,
>write your own XmlSerializer-compatible class with the same properties
as Font (or rather, those properties that are sufficient to recreate
the font), and serialize that.
I created class below and use it instead of Font class.
Is this best solution ?

Andrus.

public sealed class SerializableFont
{
// public properties are required for serialization only
public string FamilyName;
public float EmSize;

// required for serializaton only
public SerializableFont()
{
}

public SerializableFont(Font font)
{
FamilyName = font.FontFamily.Name;
EmSize = font.Size;
}

[XmlIgnore]
public Font Font
{
get
{
if (FamilyName == null || EmSize == 0)
return null;
return new Font(FamilyName, EmSize);
}
}
}

Aug 5 '08 #6
On Aug 5, 10:23*pm, "Andrus" <kobrule...@hot.eewrote:
Avoid using XmlSerializer. It's not designed to work with
arbitrary .NET classes - you pretty much to have to write classes

<specifically for it. In this case, either seialize Font by hand
<directly via XmlReader/XmlWriter (preferrably) or XmlDocument; or,
write your own XmlSerializer-compatible class with the same properties
as Font (or rather, those properties that are sufficient to recreate
the font), and serialize that.

I created class below and use it instead of Font class.
Is this best solution ?

Andrus.

* * public sealed class SerializableFont
* * {
* * * * // public properties are required for serialization only
* * * * public string FamilyName;
* * * * public float EmSize;

* * * * // required for serializaton only
* * * * public SerializableFont()
* * * * {
* * * * }

* * * * public SerializableFont(Font font)
* * * * {
* * * * * * FamilyName = font.FontFamily.Name;
* * * * * * EmSize = font.Size;
* * * * }

* * * * [XmlIgnore]
* * * * public Font Font
* * * * {
* * * * * * get
* * * * * * {
* * * * * * * * if (FamilyName == null || EmSize == 0)
* * * * * * * * * * return null;
* * * * * * * * return new Font(FamilyName, EmSize);
* * * * * * }
* * * * }
* * }
That's pretty much it, though I'd use a ToFont() method instead of
your Font property to convey the intent better - properties really
shouldn't create new objects on every gettor call; and you'll also
avoid {XmlIgnore] that way.
Aug 6 '08 #7
That's pretty much it, though I'd use a ToFont() method instead of
your Font property to convey the intent better - properties really
shouldn't create new objects on every gettor call; and you'll also
avoid {XmlIgnore] that way.

FxCop orders to use properties instead of Getter methods. Probably ToFont()
method will be
flagged as issue by FxCop.

I noticed that Project Settings Window allows to add directly Font datatype
and store/restore its values.
Font values are serialized as string as "Arial, 12"
ComponentModel automatically uses TypeConverter to serialize fonts in this
case.

Maybe it is reasonable to use TypeConverter and avoid creating wrapper
object ?

Andrus.

Aug 6 '08 #8
On Aug 6, 4:38*pm, "Andrus" <kobrule...@hot.eewrote:
FxCop orders to use properties instead of Getter methods. Probably ToFont()
method will be
flagged as issue by FxCop.
FXCop is just plain wrong there. Have a look at the BCL classes - how
many GetXxx() methods can you find. They are usually there for a
reason. One prominent example is IEnumerable.GetEnumerator() - it is a
method, and not a property, because on every call it creates a new
enumerator. The fundamental contract of a property is that, if the
state of the system does not change, calling the property gettor twice
should return the same value (i.e., foo.Bar == foo.Bar).

And no, it will not flag ToXxx() methods - would it flag, say,
ToString()? I've no idea, to be honest, since I've last used FXCop
several years ago - but if it did that, it's just not worth using. Or
maybe you should turn off that specific rule for it...
Maybe it is reasonable to use TypeConverter and avoid creating wrapper
object ?
If you do not need to serialize individual font properties to XML in
such a way that they are clearly separated in that XML (so that they
could be described by XML Schema, and easily retrieved using XPath),
then sure, using TypeConverter to convert to/from string, and storing
that string within an XML attribute or element, is a perfectly valid
solution.
Aug 6 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: RooLoo | last post by:
Hey all! Using VB.Net... How can I save the font properties of a textbox to an ASCII file and later read the file and apply the properties back to the textbox? Users use multiple workstations,...
7
by: Derrick | last post by:
Can anyone tell me how to persist and retrieve the font characteristics that a user chooses to have displayed in a personalized label field on the front of their application? I wanted to be able...
1
by: Philip K | last post by:
I was trying to use an XML file to save some program settings. But cannot serialise the settings object if it contains a Font object (even though Font is maked in the MS documentation. Dies in...
1
by: Philip K | last post by:
I was trying to use Soap serialisation to save some systems settings how ever it fails if the settings contain a Font object, works fine if the Font object is not in the object being serialised. ...
7
by: Lars-Erik Aabech | last post by:
Hi! I've got problems with serializing my collections of business objects. The objects themselves serialize fine, but the collections fail. I've got the following structure: Base collection...
5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
3
by: David Pendleton | last post by:
Hello all. Does anyone know how to persist or serialize a Font object? Possible? Thanks.
8
by: Frank Rizzo | last post by:
How do I serialize Font object into a string that I can store in either INI file or the registry (I know it’s a bad thing, but need to do it nevertheless). Then, also, how do I deserialize it from...
5
by: Terry Olsen | last post by:
I want to be able to save the user color settings in my app and restore them when the app is re-opened. I currently have a config file with different settings such as: UID=jsmith...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.