Translate

Samstag, 27. Februar 2016

C#-Komplette Klasse in XMl-Datei speichern

private void WriteItemsToXMLFile<T>(T item, string fileName)
        {
            try
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                XmlTextWriter textWriter = new XmlTextWriter(fileName, Encoding.UTF8);
                //Set formatting options:
                textWriter.Formatting = Formatting.Indented; //Intented formatting
                textWriter.Indentation = 4; //Use the size of a tab to intent
                xmlSerializer.Serialize(textWriter, item);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

Keine Kommentare:

Kommentar veröffentlichen