using System; using System.Globalization; using System.Xml; namespace MediaBrowser.Controller.Extensions { /// /// Class XmlExtensions /// public static class XmlExtensions { /// /// Safes the get int32. /// /// The doc. /// The path. /// System.Int32. public static int SafeGetInt32(this XmlDocument doc, string path) { return SafeGetInt32(doc, path, 0); } /// /// Safes the get int32. /// /// The doc. /// The path. /// The default int. /// System.Int32. public static int SafeGetInt32(this XmlDocument doc, string path, int defaultInt) { XmlNode rvalNode = doc.SelectSingleNode(path); if (rvalNode != null && rvalNode.InnerText.Length > 0) { int rval; if (Int32.TryParse(rvalNode.InnerText, out rval)) { return rval; } } return defaultInt; } /// /// The _us culture /// private static readonly CultureInfo _usCulture = new CultureInfo("en-US"); /// /// Safes the get single. /// /// The doc. /// The path. /// The min value. /// The max value. /// System.Single. public static float SafeGetSingle(this XmlDocument doc, string path, float minValue, float maxValue) { XmlNode rvalNode = doc.SelectSingleNode(path); if (rvalNode != null && rvalNode.InnerText.Length > 0) { float rval; // float.TryParse is local aware, so it can be probamatic, force us culture if (float.TryParse(rvalNode.InnerText, NumberStyles.AllowDecimalPoint, _usCulture, out rval)) { if (rval >= minValue && rval <= maxValue) { return rval; } } } return minValue; } /// /// Safes the get string. /// /// The doc. /// The path. /// System.String. public static string SafeGetString(this XmlDocument doc, string path) { return SafeGetString(doc, path, null); } /// /// Safes the get string. /// /// The doc. /// The path. /// The default string. /// System.String. public static string SafeGetString(this XmlDocument doc, string path, string defaultString) { var rvalNode = doc.SelectSingleNode(path); if (rvalNode != null) { var text = rvalNode.InnerText; return !string.IsNullOrWhiteSpace(text) ? text : defaultString; } return defaultString; } /// /// Safes the get DateTime. /// /// The doc. /// The path. /// System.DateTime. public static DateTime? SafeGetDateTime(this XmlDocument doc, string path) { return SafeGetDateTime(doc, path, null); } /// /// Safes the get DateTime. /// /// The doc. /// The path. /// The default date. /// System.DateTime. public static DateTime? SafeGetDateTime(this XmlDocument doc, string path, DateTime? defaultDate) { var rvalNode = doc.SelectSingleNode(path); if (rvalNode != null) { var text = rvalNode.InnerText; DateTime date; if (DateTime.TryParse(text, out date)) return date; } return defaultDate; } /// /// Safes the get string. /// /// The doc. /// The path. /// System.String. public static string SafeGetString(this XmlNode doc, string path) { return SafeGetString(doc, path, null); } /// /// Safes the get string. /// /// The doc. /// The path. /// The default value. /// System.String. public static string SafeGetString(this XmlNode doc, string path, string defaultValue) { var rvalNode = doc.SelectSingleNode(path); if (rvalNode != null) { var text = rvalNode.InnerText; return !string.IsNullOrWhiteSpace(text) ? text : defaultValue; } return defaultValue; } /// /// Reads the string safe. /// /// The reader. /// System.String. public static string ReadStringSafe(this XmlReader reader) { var val = reader.ReadElementContentAsString(); return string.IsNullOrWhiteSpace(val) ? null : val; } /// /// Reads the value safe. /// /// The reader. /// System.String. public static string ReadValueSafe(this XmlReader reader) { reader.Read(); var val = reader.Value; return string.IsNullOrWhiteSpace(val) ? null : val; } /// /// Reads a float from the current element of an XmlReader /// /// The reader. /// System.Single. public static float ReadFloatSafe(this XmlReader reader) { string valueString = reader.ReadElementContentAsString(); float value = 0; if (!string.IsNullOrWhiteSpace(valueString)) { // float.TryParse is local aware, so it can be probamatic, force us culture float.TryParse(valueString, NumberStyles.AllowDecimalPoint, _usCulture, out value); } return value; } /// /// Reads an int from the current element of an XmlReader /// /// The reader. /// System.Int32. public static int ReadIntSafe(this XmlReader reader) { string valueString = reader.ReadElementContentAsString(); int value = 0; if (!string.IsNullOrWhiteSpace(valueString)) { int.TryParse(valueString, out value); } return value; } } }