public static int SafeGetIntFromElement(XElement element, string elementName) { XElement x = GetElement(element, elementName); int result; if (!int.TryParse(x.Value, out result)) { throw new InvalidOperationException(string.Format("Decimal not found in element {0}.", elementName)); } return result; }