XmlExtensions.cs 1.3 KB

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