예제 #1
0
        public Diary LoadXml(string inputDiaryXmlFilename)
        {
            try
            {
                var xmlFile = new XmlFileStreamSerialize {
                    Filename = inputDiaryXmlFilename
                };

                Debug.WriteLine($"Loading diary: {xmlFile.Filename}");
                using (var fileReader = new StreamReader(xmlFile.Filename))
                {
                    this.Diary = xmlFile.Deserialize(fileReader);
                    return(Diary);
                }
            }
            catch (Exception ex)
            {
                log.FatalFormat("Error loading the xml from file {0}. {1}", inputDiaryXmlFilename, ex);
                if (ex.InnerException != null)
                {
                    log.Fatal("InnerException:", ex.InnerException);
                }
                throw;
            }
        }
예제 #2
0
        public void TestValidDiary(string outpath, Diary diary)
        {
            this.SaveXml(outpath, diary);
            var xmlFile = new XmlFileStreamSerialize {
                Filename = outpath
            };

            try
            {
                using (var fileReader = new StreamReader(xmlFile.Filename))
                {
                    xmlFile.Deserialize(fileReader);
                }
            }
            catch (XmlException xmlEx)
            {
                var offendingLine = File.ReadLines(outpath).ElementAtOrDefault(xmlEx.LineNumber - 2);
                var message       = string.Format("The diary content has invalid XML. {1}{0}{0}{2}{0}{3}",
                                                  Environment.NewLine,
                                                  xmlEx.Message,
                                                  offendingLine, xmlFile.Filename);

                throw new InvalidOperationException(message);
            }
        }
예제 #3
0
        private void SaveXml(string outpath, Diary diary)
        {
            var xmlFileStreamSerialize = new XmlFileStreamSerialize();

            using (var fileStream = new StreamWriter(outpath))
            {
                xmlFileStreamSerialize.Serialize(fileStream, diary);
                Debug.WriteLine($"Saving to {outpath}");
            }
        }
예제 #4
0
        public static Diary LoadXml(string inputDiaryXmlFilename)
        {
            var xmlFile = new XmlFileStreamSerialize {
                Filename = inputDiaryXmlFilename
            };

            Debug.WriteLine($"Loading diary: {xmlFile.Filename}");
            using (var fileReader = new StreamReader(xmlFile.Filename))
            {
                return(xmlFile.Deserialize(fileReader));
            }
        }