private Config InitConfiguration(string filename)
{
try
{
XDocument xDocument = XDocument.Load(filename);
return CreateObjectsFromString<Config>(xDocument);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return null;
}
}
private T CreateObjectsFromString <T> (XDocument xDocument)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
return (T)xmlSerializer.Deserialize(new StringReader(xDocument.ToString()));
}
{
try
{
XDocument xDocument = XDocument.Load(filename);
return CreateObjectsFromString<Config>(xDocument);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return null;
}
}
private T CreateObjectsFromString <T> (XDocument xDocument)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
return (T)xmlSerializer.Deserialize(new StringReader(xDocument.ToString()));
}
Benutzen:
Config config = InitConfiguration(AppDomain.CurrentDomain.BaseDirectory + "Config.xml");
Die Klasse Config:
public class Config
{
private string chars;
public string Chars
{
get
{
return chars;
}
set
{
chars = value;
}
}
private double density;
public double Density
{
get
{
return density;
}
set
{
density = value;
}
}
}
{
private string chars;
public string Chars
{
get
{
return chars;
}
set
{
chars = value;
}
}
private double density;
public double Density
{
get
{
return density;
}
set
{
density = value;
}
}
}
Keine Kommentare:
Kommentar veröffentlichen