jellyfin-server/MediaBrowser.Controller/Xml/XmlExtensions.cs

48 lines
1.4 KiB
C#
Raw Normal View History

using System.Globalization;
using System.Threading.Tasks;
2012-07-12 06:55:27 +00:00
using System.Xml;
namespace MediaBrowser.Controller.Xml
{
public static class XmlExtensions
{
private static CultureInfo _usCulture = new CultureInfo("en-US");
2012-07-12 06:55:27 +00:00
/// <summary>
/// Reads a float from the current element of an XmlReader
/// </summary>
public static async Task<float> ReadFloatSafe(this XmlReader reader)
2012-07-12 06:55:27 +00:00
{
string valueString = await reader.ReadElementContentAsStringAsync();
2012-07-12 06:55:27 +00:00
float value = 0;
2012-07-12 06:55:27 +00:00
if (!string.IsNullOrWhiteSpace(valueString))
2012-07-12 06:55:27 +00:00
{
// float.TryParse is local aware, so it can be probamatic, force us culture
float.TryParse(valueString, NumberStyles.AllowDecimalPoint, _usCulture, out value);
2012-07-12 06:55:27 +00:00
}
return value;
2012-07-12 06:55:27 +00:00
}
/// <summary>
/// Reads an int from the current element of an XmlReader
/// </summary>
public static async Task<int> ReadIntSafe(this XmlReader reader)
2012-07-12 06:55:27 +00:00
{
string valueString = await reader.ReadElementContentAsStringAsync();
2012-07-12 06:55:27 +00:00
int value = 0;
if (!string.IsNullOrWhiteSpace(valueString))
2012-07-12 06:55:27 +00:00
{
int.TryParse(valueString, out value);
2012-07-12 06:55:27 +00:00
}
return value;
2012-07-12 06:55:27 +00:00
}
}
}