Пример #1
0
        public static void SaveFile(string filePath, CourseFile courseFile, bool isBackup = true, int coutBackups = 10)
        {
            if (isBackup && File.Exists(filePath))
            {
                var fileName = Path.GetFileName(filePath);
                var fileDir  = Path.GetDirectoryName(filePath);
                RemoveOldBackups(fileDir, coutBackups);
                var fileBackName = string.Format("{0}.{1}{2}",
                                                 fileName,
                                                 string.Format("{0:" + BackupDateTimeFormat + "}", DateTime.Now),
                                                 BackupExtension);
                var fileBackPath = Path.Combine(fileDir, fileBackName);
                File.Copy(filePath, fileBackPath);
            }

            SaveAsFile(filePath, courseFile);
        }
Пример #2
0
        public static void SaveAsFile(string filePath, CourseFile courseFile)
        {
            using (var streamMiddle = new MemoryStream())
                using (var xmlWriterMiddle = XmlWriter.Create(streamMiddle))
                {
                    //Create our own namespaces for the output
                    var namespaces = new XmlSerializerNamespaces();
                    //Add an empty namespace and empty value
                    namespaces.Add("", "");

                    SerializerCourseFile.Serialize(xmlWriterMiddle, courseFile, namespaces);
                    streamMiddle.Flush();
                    streamMiddle.Seek(0, SeekOrigin.Begin);

                    using (var xmlReader = XmlReader.Create(streamMiddle))
                    {
                        using (var streamOut = new FileStream(filePath, FileMode.Create))
                        {
                            TransformToCourse.TransformToXmlStream(xmlReader, streamOut);
                        }
                    }
                }
        }