Click here to Skip to main content
15,436,928 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My requirement is to save user font selection to an XML file and retrieve back.
What i have done so far is.
In my form there is two Font named as Fnt and ChqDateF
The method i used to write to an XML file is
C#
new XAttribute("F", ChqDateF==null ? Fnt:ChqDateF)

and the result i am getting is
XML
F="[Font: Name=Tahoma, Size=19.75, Units=3, GdiCharSet=1, GdiVerticalFont=False]"

From this string i want to retrieve FontName and FontSize.
In the above string FontStyle is not getting.I need FontStyle also
Whats the best way to achieve this task
Please Guide me
Posted

Better don't read/write XML manually, especially of you just need to start some set of options. Everything is already done for you. Please see: http://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx[^].

Basically, you just create some data type (or even a set of type to form complex data structure, forming any thinkable object graph, which does not even have to be a tree) and mark the types and the members you want to be a part of contract with DataContract and DataMember attributes. The serializer will automatically save the data to any stream when you need it, and will later restore the data exactly as it was before. You don't even need to know how is that represented in XML.

—SA
 
Share this answer
 
Well you can use Xaml here is the code.
C#
public static FonSettings Load(string file)
{
    var p = XamlServices.Load(file) as FonSettings;
    return p;
}

public static void Save(string file, ref FonSettings pf)
{
    File.WriteAllText(file, XamlServices.Save(pf));
}

and here is the FonSettings class
SQL
public class FonSettings
{
    public Font FontToSave { get; set; }
}

all you do is load and save the file and it will return the class with setting every time.
Hope it was help full.
Edit:
NOTE: you must add reference to the System.Xaml dll that is in dotnet
You can save all kinde of variables using this method.
 
Share this answer
 
v2
Comments
jinesh sam 5-Nov-14 1:29am    
string file means the entire xml or "[Font: Name=Tahoma, Size=19.75, Units=3, GdiCharSet=1, GdiVerticalFont=False]"
Myvar 5-Nov-14 1:40am    
No it will literally save all variables in the class to a xaml(almost like xml) to a file and load it it is a realy great way to save data and restore it you can add as much variables as you like to the class an load and save it the file parameter is the path to file to save the xaml eg: Save(@"C:\myfile.xml",new FonSettings(){FontToSave = (your font)});
One solution is to use Type Convertor[^] like in the next example:
C#
Font myFont = new Font("Arial", 12, FontStyle.Italic);
TypeConverter converter = TypeDescriptor.GetConverter(typeof(Font));
//
// Saving Font object as a string
string fontString = converter.ConvertToString(myFont);
//
// ...Then use this string into your XML
//
// Load an instance of Font from a string
Font font = (Font)converter.ConvertFromString(fontString);
 
Share this answer
 
v2
Your code doesn't show exactly how you are writing the XML, but this will get you a string with a compete Font description including Style, and you can also convert to a Font from such a description:
C#
// required
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

private TypeConverter fontConverter = TypeDescriptor.GetConverter(typeof(Font));

private string fontToFontDescription(Font font)
{
    return fontConverter.ConvertToString(font);
}

private Font fontDescriptionToFont(string fdescription)
{
    // edit: correct code inserted
    return fontConverter.ConvertFromString(fdescription) as Font;
}
Interestingly, if you do this test:
C#
//execute inside some method
string fstr = fontToFontDescription(textBox1.Font);

Font fnt = fontDescriptionToFont(fstr);

bool eq = fnt.Equals(textBox1.Font);
The result is 'false: the return value of the conversion has 'GdiCharSet: 1; the font in the TextBox has GdiCharSet: 0. On a practical level, you can go ahead and use the result of the conversion with results you'd expect.

Will using the Font converted to string in this way solve your problem ?
 
Share this answer
 
v2
Comments
jinesh sam 5-Nov-14 2:08am     CRLF
I am getting this error<br/> System.ComponentModel.TypeConverter' does not contain a definition for 'ConvertToFont' and no extension method 'ConvertToFont' accepting a first argument of type 'System.ComponentModel.TypeConverter
BillWoodruff 5-Nov-14 4:10am    
Hi, I'm not sure how this happened, but somehow the working code I thought I pasted into my reply is not what's there now. Fixed. sorry about that, Bill
jinesh sam 5-Nov-14 13:45pm    
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900