Translate

Sonntag, 25. Oktober 2015

XMLParsing mit C#

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()));
        }

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;
            }
        }
    }

Keine Kommentare:

Kommentar veröffentlichen