public bool TryGetElement(string elementSymbol, out Element element) { return _elements.TryGetValue(elementSymbol, out element); }
/// <summary> /// Adds an element to this periodic table if the element atomic symbol is not already present. /// Overrides an element if the symbol is already present. /// </summary> /// <param name="element">The element to add to the periodic table</param> /// <returns>True if the element was not present before, false if the element existed and was overwritten</returns> public bool AddElement(Element element) { if (_elements.ContainsKey(element.AtomicSymbol)) { _elements[element.AtomicSymbol] = element; return false; } _elements.Add(element.AtomicSymbol, element); // Special case for Deuterium if (element.AtomicSymbol == "H") { _elements.Add("D", element); } return true; }
/// <summary> /// Load a xml file containing elemental and isotopic data into the periodic table /// </summary> public void LoadElements(Stream elementsListXml) { using (XmlReader reader = XmlReader.Create(elementsListXml)) { reader.ReadToFollowing("PeriodicTable"); _uniqueId = int.Parse(reader.GetAttribute("defaultID")); int isotopes = int.Parse(reader.GetAttribute("isotopesCount")); _isotopes = new Isotope[isotopes]; while (reader.ReadToFollowing("Element")) { reader.ReadToFollowing("Name"); string name = reader.ReadElementContentAsString(); reader.ReadToFollowing("Symbol"); string symbol = reader.ReadElementContentAsString(); reader.ReadToFollowing("AtomicNumber"); int atomicnumber = reader.ReadElementContentAsInt(); reader.ReadToFollowing("ValenceElectrons"); int valenceElectrons = reader.ReadElementContentAsInt(); Element element = new Element(name, symbol, atomicnumber, valenceElectrons); bool isStartNode = reader.ReadToNextSibling("Isotope"); while(isStartNode) { string unqiueId = reader.GetAttribute("uniqueID"); reader.ReadToFollowing("Mass"); double mass = reader.ReadElementContentAsDouble(); reader.ReadToFollowing("MassNumber"); int massNumber = reader.ReadElementContentAsInt(); reader.ReadToFollowing("RelativeAbundance"); float abundance = reader.ReadElementContentAsFloat(); if (abundance > 0) { Isotope isotope = element.AddIsotope(massNumber, mass, abundance); if (unqiueId != null) { int uniqueId = int.Parse(unqiueId); isotope.UniqueId = uniqueId; _isotopes[uniqueId] = isotope; } else { isotope.UniqueId = _uniqueId; _isotopes[_uniqueId++] = isotope; } } if (!reader.IsStartElement("Isotope")) isStartNode = reader.ReadToNextSibling("Isotope"); } AddElement(element); } } if(_isotopes.Length != _uniqueId) Array.Resize(ref _isotopes, _uniqueId); }
/// <summary> /// Load a xml file containing elemental and isotopic data into the periodic table /// </summary> public static void Load(string filePath) { _elements.Clear(); Element element = null; _isotopes = new Isotope[500]; BiggestIsotopeNumber = -1; using (XmlReader reader = XmlReader.Create(filePath)) { while (reader.Read()) { if (!reader.IsStartElement()) continue; switch (reader.Name) { case "Element": string name = reader["Name"]; string symbol = reader["Symbol"]; int atomicnumber = int.Parse(reader["AtomicNumber"]); int valenceElectrons = int.Parse(reader["ValenceElectrons"]); element = new Element(name, symbol, atomicnumber, valenceElectrons); AddElement(element); break; case "Isotope": string unqiueId = reader["Id"]; string a = reader["Mass"]; double mass = double.Parse(reader["Mass"], CultureInfo.CurrentCulture); int massNumber = int.Parse(reader["MassNumber"]); float abundance = float.Parse(reader["Abundance"], CultureInfo.CurrentCulture); if (abundance > 0 && element != null) { Isotope isotope = element.AddIsotope(massNumber, mass, abundance); if (unqiueId != null) { int uniqueId = int.Parse(unqiueId); if (uniqueId > BiggestIsotopeNumber) BiggestIsotopeNumber = uniqueId; isotope.UniqueId = uniqueId; _isotopes[uniqueId] = isotope; } else { isotope.UniqueId = BiggestIsotopeNumber; _isotopes[BiggestIsotopeNumber++] = isotope; } } break; } } } if (_isotopes.Length > BiggestIsotopeNumber) Array.Resize(ref _isotopes, BiggestIsotopeNumber + 1); }