XmlExtensions.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System.Globalization;
  2. using System.Threading.Tasks;
  3. using System.Xml;
  4. namespace MediaBrowser.Controller.Xml
  5. {
  6. public static class XmlExtensions
  7. {
  8. private static CultureInfo _usCulture = new CultureInfo("en-US");
  9. /// <summary>
  10. /// Reads a float from the current element of an XmlReader
  11. /// </summary>
  12. public static float ReadFloatSafe(this XmlReader reader)
  13. {
  14. string valueString = reader.ReadElementContentAsString();
  15. float value = 0;
  16. if (!string.IsNullOrWhiteSpace(valueString))
  17. {
  18. // float.TryParse is local aware, so it can be probamatic, force us culture
  19. float.TryParse(valueString, NumberStyles.AllowDecimalPoint, _usCulture, out value);
  20. }
  21. return value;
  22. }
  23. /// <summary>
  24. /// Reads an int from the current element of an XmlReader
  25. /// </summary>
  26. public static int ReadIntSafe(this XmlReader reader)
  27. {
  28. string valueString = reader.ReadElementContentAsString();
  29. int value = 0;
  30. if (!string.IsNullOrWhiteSpace(valueString))
  31. {
  32. int.TryParse(valueString, out value);
  33. }
  34. return value;
  35. }
  36. }
  37. }