public static XmlDocument WriteRepository(ICurricularUnitFormRepository<CurricularUnitForm> cufr) { XmlDocument xmlFile = new XmlDocument(); XmlDeclaration decl = xmlFile.CreateXmlDeclaration("1.0", "utf-8", null); xmlFile.InsertBefore(decl, xmlFile.DocumentElement); XmlDocumentType type = xmlFile.CreateDocumentType("fuc-repository", "template.dtd", null, null); xmlFile.AppendChild(type); XmlElement fuc_repo = xmlFile.CreateElement("fuc-repository"); fuc_repo.SetAttribute("school", "Instituto Superior de Engenharia de Lisboa"); fuc_repo.SetAttribute("language", "pt"); xmlFile.AppendChild(fuc_repo); foreach (CurricularUnitForm cuf in cufr.GetAll()) { /*http://stackoverflow.com/questions/982597/what-is-the-fastest-way-to-combine-two-xml-files-into-one*/ var xmlTmp = xmlFile.ImportNode(Write(cuf, false).DocumentElement, true); fuc_repo.AppendChild(xmlTmp); } return xmlFile; }
public static bool Load(ICurricularUnitFormRepository<CurricularUnitForm> _repo, String _filePath) { try { CurricularUnitForm cuf = new CurricularUnitForm(); if (_repo == null || _filePath == null) return false; XmlTextReader xmlFile = new XmlTextReader(_filePath); //xmlFile.Read(); while (xmlFile.Read()) { xmlFile.MoveToElement(); if (xmlFile.NodeType == XmlNodeType.Element && xmlFile.Name.Equals("fuc")) { ulong id = ulong.Parse(xmlFile.GetAttribute("id")); XmlReader xmlTree = xmlFile.ReadSubtree(); while (xmlTree.Read()) { if (xmlFile.NodeType == XmlNodeType.Element) { switch (xmlTree.Name) { case "uc": String acr = xmlFile.GetAttribute("acronym"); cuf = _repo.GetByAcronym(acr); if (cuf == null) cuf = new CurricularUnitForm(xmlFile.GetAttribute("name"), acr); cuf.CUnit.Name = xmlFile.GetAttribute("name"); cuf.ID = id; try { cuf.CUnit.ECTS = Convert.ToDouble(xmlFile.GetAttribute("ects")); } catch (FormatException e) { cuf.CUnit.ECTS = 0; } break; case "ruc": acr = xmlFile.GetAttribute("acronym"); CurricularUnitForm cuf_aux = _repo.GetByAcronym(acr); if (cuf_aux == null) cuf_aux = new CurricularUnitForm(xmlFile.GetAttribute("name"), acr); _repo.Add(cuf_aux); cuf.AddRequiredCourses(cuf_aux); break; case "type-uc": cuf.Type.CourseType = (CourseType)Enum.Parse(typeof(CourseType), xmlFile.GetAttribute("courseType")); cuf.Type.Semester = (Semester)Enum.Parse(typeof(Semester), xmlFile.GetAttribute("semester")); cuf.Type.Degree = (Degree)Enum.Parse(typeof(Degree), xmlFile.GetAttribute("degree")); break; case "objectives": cuf.Description.Objectives = xmlFile.ReadString(); break; case "learningResults": cuf.Description.LearningResults = xmlFile.ReadString(); break; case "resultEvaluation": cuf.Description.ResultEvaluation = xmlFile.ReadString(); break; case "courseProgram": cuf.Description.CourseProgram = xmlFile.ReadString(); break; case "language": cuf.Description.Language = xmlFile.ReadString(); break; } } } if (cuf.Type.Degree.Equals(Degree.EIC)) _repo.Add(cuf); } } } catch (Exception e) { Console.WriteLine(e); return false; } return true; }
public static bool WriteRepositoryToFile(ICurricularUnitFormRepository<CurricularUnitForm> cufr, string filePath) { try { XmlDocument doc = WriteRepository(cufr); XmlTextWriter writer = new XmlTextWriter(filePath, null); writer.Formatting = Formatting.Indented; doc.Save(writer); } catch (Exception e) { Console.WriteLine(e); return false; } return true; }