/// <summary> /// Loads the specified <see cref="IXmlReadable"/> object from an xml stream /// </summary> /// <param name="obj">Object to serialize from xml</param> /// <param name="stream">Stream to load from</param> public static void LoadXml(this IXmlReadable obj, Stream stream) { var doc = new XmlDocument(); doc.Load(stream); obj.ReadXml(doc.DocumentElement); }
/// <summary> /// Loads the specified <see cref="IXmlReadable"/> object from an xml file /// </summary> /// <param name="obj">Object to serialize from xml</param> /// <param name="fileName">File to load from</param> public static void LoadXml(this IXmlReadable obj, string fileName) { using (var fileStream = File.OpenRead(fileName)) { LoadXml(obj, fileStream); } }
/// <summary> /// Saves the specified <see cref="IXmlReadable"/> object to an xml stream /// </summary> /// <param name="obj">Object to serialize to xml</param> /// <param name="stream">Stream to save as</param> /// <param name="documentElementName">Document element name</param> public static void SaveXml(this IXmlReadable obj, Stream stream, string documentElementName = "object") { var doc = new XmlDocument(); var topNode = doc.CreateElement(documentElementName); obj.WriteXml(topNode); doc.AppendChild(topNode); doc.Save(stream); }
/// <summary> /// Saves the specified <see cref="IXmlReadable"/> object to an xml file /// </summary> /// <param name="obj">Object to serialize to xml</param> /// <param name="fileName">File to save as</param> /// <param name="documentElementName">Document element name</param> public static void SaveXml(this IXmlReadable obj, string fileName, string documentElementName = "object") { using (var stream = new MemoryStream()) { SaveXml(obj, stream, documentElementName); stream.Position = 0; using (var fileStream = File.Create(fileName)) { stream.WriteTo(fileStream); } } }
/// <summary> /// Saves the specified <see cref="IXmlReadable"/> object to an xml file /// </summary> /// <param name="obj">Object to serialize to xml</param> /// <param name="fileName">File to save as</param> /// <param name="documentElementName">Document element name</param> public static void SaveXml(this IXmlReadable obj, string fileName, string documentElementName = "object") { using (var fileStream = File.Create(fileName)) { SaveXml(obj, fileStream, documentElementName); } }