From 5e08d95b92bccd86109a11a347746d643531a1ed Mon Sep 17 00:00:00 2001 From: LukePulverenti Date: Thu, 21 Feb 2013 12:22:22 -0500 Subject: fixed x64 --- .../Extensions/XmlExtensions.cs | 201 +++++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 MediaBrowser.Controller/Extensions/XmlExtensions.cs (limited to 'MediaBrowser.Controller/Extensions/XmlExtensions.cs') diff --git a/MediaBrowser.Controller/Extensions/XmlExtensions.cs b/MediaBrowser.Controller/Extensions/XmlExtensions.cs new file mode 100644 index 000000000..00bee7d1d --- /dev/null +++ b/MediaBrowser.Controller/Extensions/XmlExtensions.cs @@ -0,0 +1,201 @@ +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) + { + XmlNode rvalNode = doc.SelectSingleNode(path); + if (rvalNode != null && rvalNode.InnerText.Trim().Length > 0) + { + return rvalNode.InnerText; + } + return defaultString; + } + + /// + /// 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) + { + XmlNode rvalNode = doc.SelectSingleNode(path); + if (rvalNode != null && rvalNode.InnerText.Length > 0) + { + return rvalNode.InnerText; + } + 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; + } + } +} \ No newline at end of file -- cgit v1.2.3