// CONSTRUCTEUR /// <summary> /// Creates an instance of the XmlRulesBaseParser /// </summary> public XmlRulesBaseParser() { name = null; version = 0.0f; date = DateTime.Today; rulesBase = new RulesBase(); errorCode = XmlRulesBaseParserErrorCode.SUCCESS; }
/// <summary> /// Creates and returns RulesBase object from the given XML file, argument must be a valid xml file path, otherwise null is returned /// </summary> /// <param name="a_source">XML Source file path</param> /// <returns>RulesBase object generated from an XML file, null is returned if parsing is unsuccessful</returns> public RulesBase CreateRulesBase(object a_source) { if (a_source != null) { try { string fileName = (string)a_source; XDocument xmlDoc = XDocument.Load(fileName); var ruleElements = from XElement element in xmlDoc.Descendants("Rule") select element; foreach (XElement element in ruleElements) { Rule newRule = ParseRule(element); if (newRule == null) { throw new NullReferenceException(); // Pour affecter errorCode => voir catch } rulesBase.Rules.Add(newRule); } name = xmlDoc.Descendants("RulesBaseInfo").FirstOrDefault().Descendants("Name").FirstOrDefault().Value; float version = 0.0f; if (float.TryParse(xmlDoc.Descendants("RulesBaseInfo").FirstOrDefault().Descendants("Issue").FirstOrDefault().Value, out version)) { this.version = version; } int day = 1; bool isDayCorrect = int.TryParse(xmlDoc.Descendants("RulesBaseInfo").FirstOrDefault().Descendants("Date").FirstOrDefault().Attribute("day").Value, out day); int month = 1; bool isMonthCorrect = int.TryParse(xmlDoc.Descendants("RulesBaseInfo").FirstOrDefault().Descendants("Date").FirstOrDefault().Attribute("month").Value, out month); int year = 0; bool isYearCorrect = int.TryParse(xmlDoc.Descendants("RulesBaseInfo").FirstOrDefault().Descendants("Date").FirstOrDefault().Attribute("year").Value, out year); if (isDayCorrect && isMonthCorrect && isYearCorrect) { try { date = new DateTime(year, month, day); } catch (Exception) { // Format de la date incorrect // Pas d'action, donnée non essentielle } } } catch (XmlException) { errorCode = XmlRulesBaseParserErrorCode.ERROR_NOT_XML_FILE; rulesBase = null; } catch (NullReferenceException) { errorCode = XmlRulesBaseParserErrorCode.ERROR_INCOMPLETE_XML_FILE; rulesBase = null; } catch (Exception ex) { Console.WriteLine(ex.GetType() + " " + ex.Message + " " + ex.Source.ToString()); errorCode = XmlRulesBaseParserErrorCode.ERROR_UNKNOWN; rulesBase = null; } } return(rulesBase); }