using System;
using System.Globalization;
using System.Xml;
namespace MediaBrowser.Common.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;
}
}
}