wyb
2021-05-11 49ce087bd2a34a150597e1cc1da157af242c0b6d
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
 
namespace Pcb.Common.Extension
{
    public static class XmlExtension
    {
        public static string GetStringValueOrDefault(this XmlAttribute attr, string defaultValue = null)
        {
            if (attr != null)
            {
                return attr.Value.Trim();
            }
 
            return defaultValue;
        }
 
        public static decimal? GetNullableDecimalValueOrDefault(this XmlAttribute attr, decimal? defaultValue = null)
        {
            var value = attr.GetStringValueOrDefault();
            if (value == null)
            {
                return defaultValue;
            }
 
            var d = 0m;
            if (decimal.TryParse(value, out d))
            {
                return d;
            }
            else
            {
                return defaultValue;
            }
        }
 
        public static int GetInt32ValueOrDefault(this XmlAttribute attr, int defaultValue = 0)
        {
            var value = attr.GetStringValueOrDefault();
            if (value == null)
            {
                return defaultValue;
            }
 
            var d = 0;
            if (int.TryParse(value, out d))
            {
                return d;
            }
            else
            {
                return defaultValue;
            }
        }
 
        /// <summary>
        /// 获取int32的值,如果不能转换则抛出异常
        /// </summary>
        /// <param name="attr"></param>
        /// <returns></returns>
        public static int GetInt32Value(this XmlAttribute attr)
        {
            return int.Parse(attr.Value);
        }
 
        public static bool IsAttributeNotNullAndEmpty(this XmlNode node, string attrName)
        {
            var attr = node.Attributes[attrName];
            if (attr == null || string.IsNullOrWhiteSpace(attr.Value))
            {
                return false;
            }
 
            return true;
        }
    }
}