aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Controller/Xml/XmlExtensions.cs
blob: e706baa61461deb1256c89bc6c59faa71cb1ab8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System.Globalization;
using System.Xml;

namespace MediaBrowser.Controller.Xml
{
    public static class XmlExtensions
    {
        private static CultureInfo _usCulture = new CultureInfo("en-US");

        /// <summary>
        /// Reads a float from the current element of an XmlReader
        /// </summary>
        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;
        }

        /// <summary>
        /// Reads an int from the current element of an XmlReader
        /// </summary>
        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;
        }
    }
}