CSharp examples for System.Xml:XML File
Reads an object instance from an XML file. Object type must have a parameterless constructor.
using System.Xml.Serialization; using System.Threading.Tasks; using System.Text; using System.Linq; using System.IO;// www . ja v a 2s . com using System.Collections.Generic; using System; public class Main{ /// <summary> /// Reads an object instance from an XML file. /// <para>Object type must have a parameterless constructor.</para> /// </summary> /// <typeparam name="T">The type of object to read from the file.</typeparam> /// <param name="filePath">The file path to read the object instance from.</param> /// <returns>Returns a new instance of the object read from the XML file.</returns> public static T ReadFromXmlFile<T>(string filePath) where T : new() { FileWriter.IsFileLocked(filePath); TextReader reader = null; try { var serializer = new XmlSerializer(typeof(T)); reader = new StreamReader(filePath); return (T)serializer.Deserialize(reader); } finally { if (reader != null) reader.Close(); } } }