//Reads data from a schedule XML file public static Schedule ReadSchedule(string filePath) { if (!File.Exists(filePath)) { Schedule s = new Schedule(); for (int i = 0; i < 7; i++) { DailyVideoFiles d = new DailyVideoFiles(); s.DailyVideoFilesList.Add(d); } XMLWriter.WriteSchedule(s, filePath); } Schedule schedule = new Schedule(); List<DailyVideoFiles> dailyVideoFilesList = new List<DailyVideoFiles>(); XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(filePath); XmlNodeList xmlNodeList_Day = xmldoc.ChildNodes; XmlNodeList xmlNodeList_Program; XmlNode xmlNode_Day = xmlNodeList_Day[1]; XmlNode xmlNode_Program; if (xmlNode_Day.Name == "schedule") { xmlNodeList_Day = xmlNode_Day.ChildNodes; DailyVideoFiles dailyVideoFiles = new DailyVideoFiles(); for (int i = 0; i < 7; i++) { dailyVideoFiles = new DailyVideoFiles(); xmlNode_Day = xmlNodeList_Day[i]; if (xmlNode_Day.Name == "day") { //System.Windows.MessageBox.Show("day"); List<VideoFile> videoFileList = new List<VideoFile>(); xmlNodeList_Program = xmlNode_Day.ChildNodes; for (int j = 0; j < xmlNodeList_Program.Count; j++) { //videoFileList = new List<VideoFile>(); if (j == 0) { xmlNode_Program = xmlNodeList_Program[0]; if (xmlNode_Program.Name == "date") dailyVideoFiles.Date = xmlNode_Program.InnerText; else throw new XMLFileFormatException("missing \"date\" element"); } else { xmlNode_Program = xmlNodeList_Program[j]; if (xmlNode_Program.Name == "program") { //System.Windows.MessageBox.Show("program"); XmlNodeList xmlNodeList1 = xmlNode_Program.ChildNodes; VideoFile videoFile = new VideoFile(); foreach (XmlNode xmlNode1 in xmlNodeList1) { if (xmlNode1.Name == "path") videoFile.Path = xmlNode1.InnerXml; else if (xmlNode1.Name == "name") videoFile.Name = xmlNode1.InnerXml; else if (xmlNode1.Name == "ext") videoFile.Ext = xmlNode1.InnerXml; else if (xmlNode1.Name == "length") videoFile.Length = xmlNode1.InnerXml; else if (xmlNode1.Name == "subtitles") videoFile.Subtitles = xmlNode1.InnerXml; else if (xmlNode1.Name == "start_time") videoFile.Start_Time = xmlNode1.InnerXml; else if (xmlNode1.Name == "end_time") videoFile.End_Time = xmlNode1.InnerXml; else if (xmlNode1.Name == "category") videoFile.Category = xmlNode1.InnerXml; else if (xmlNode1.Name == "col") videoFile.Col = int.Parse(xmlNode1.InnerXml); else if (xmlNode1.Name == "row") videoFile.Row = int.Parse(xmlNode1.InnerXml); else if (xmlNode1.Name == "index") videoFile.Index = int.Parse(xmlNode1.InnerXml); else throw new XMLFileFormatException("unknown \"" + xmlNode1.Name + "\" element"); } videoFileList.Add(videoFile); } else { throw new XMLFileFormatException("missing \"program\" element"); } } } //end reading programs in a day dailyVideoFiles.VideoFileList = videoFileList; } else { throw new XMLFileFormatException("missing \"day\" element"); } //end reading every day's programs in a week if (dailyVideoFiles.VideoFileList.Count != 0) { schedule.DailyVideoFilesList.Add(dailyVideoFiles); } } } else throw new XMLFileFormatException("missing \"schedule\" element"); return schedule; }
//Writes data to a schedule XML file public static void WriteSchedule(Schedule schedule, string filePath) { XmlDocument xmlDoc = new XmlDocument(); XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "", ""); dec.Encoding = "UTF-8"; xmlDoc.AppendChild(dec); XmlElement body = xmlDoc.CreateElement("schedule"); xmlDoc.AppendChild(body); foreach (DailyVideoFiles dailyVideoFiles in schedule.DailyVideoFilesList) { XmlElement day = xmlDoc.CreateElement("day"); body.AppendChild(day); XmlElement date = xmlDoc.CreateElement("date"); day.AppendChild(date); date.InnerText = dailyVideoFiles.Date; foreach (VideoFile videoFile in dailyVideoFiles.VideoFileList) { XmlElement program = xmlDoc.CreateElement("program"); day.AppendChild(program); XmlElement path = xmlDoc.CreateElement("path"); program.AppendChild(path); path.InnerText = videoFile.Path; XmlElement name = xmlDoc.CreateElement("name"); program.AppendChild(name); name.InnerText = videoFile.Name; XmlElement ext = xmlDoc.CreateElement("ext"); program.AppendChild(ext); ext.InnerText = videoFile.Ext; XmlElement subtitles = xmlDoc.CreateElement("subtitles"); program.AppendChild(subtitles); if (videoFile.Subtitles != "") subtitles.InnerText = videoFile.Subtitles; XmlElement length = xmlDoc.CreateElement("length"); program.AppendChild(length); length.InnerText = videoFile.Length; XmlElement start_time = xmlDoc.CreateElement("start_time"); program.AppendChild(start_time); start_time.InnerText = videoFile.Start_Time; XmlElement end_time = xmlDoc.CreateElement("end_time"); program.AppendChild(end_time); end_time.InnerText = videoFile.End_Time; XmlElement col = xmlDoc.CreateElement("col"); program.AppendChild(col); col.InnerText = videoFile.Col.ToString(); XmlElement row = xmlDoc.CreateElement("row"); program.AppendChild(row); row.InnerText = videoFile.Row.ToString(); XmlElement index = xmlDoc.CreateElement("index"); program.AppendChild(index); index.InnerText = videoFile.Index.ToString(); XmlElement category = xmlDoc.CreateElement("category"); program.AppendChild(category); if (videoFile.Category != "") category.InnerText = videoFile.Category; } } xmlDoc.Save(filePath); }