using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; namespace Bowin.Common.XML { public static partial class XElementExtensions { public static double? GetDoubleValue(this XElement node) { if (node == null) return null; if (string.IsNullOrEmpty(node.Value)) return null; return double.Parse(node.Value); } public static DateTime? GetDateTimeValue(this XElement node) { if (node == null) return null; if (string.IsNullOrEmpty(node.Value)) return null; return DateTime.Parse(node.Value); } public static string GetTrimStringValue(this XElement node) { if (node == null) return string.Empty; return node.Value.Trim(); } public static string GetStringValue(this XElement node) { if (node == null) return string.Empty; return node.Value; } public static List GetStringListValue(this XElement node, char splitChar = ',') { var values = default(List); if (node != default(XElement)) { values = node.Value.Split(new char[] { splitChar }, StringSplitOptions.RemoveEmptyEntries).ToList(); } return values; } public static decimal? GetDecimalValue(this XElement node) { if (node == null || string.IsNullOrEmpty(node.Value)) return null; if (string.IsNullOrEmpty(node.Value)) return null; return decimal.Parse(node.Value); } public static int? GetIntValue(this XElement node) { if (node == null || string.IsNullOrEmpty(node.Value)) return null; if (string.IsNullOrEmpty(node.Value)) return null; return int.Parse(node.Value); } public static long? GetLongValue(this XElement node) { if (node == null || string.IsNullOrEmpty(node.Value)) return null; if (string.IsNullOrEmpty(node.Value)) return null; return long.Parse(node.Value); } public static List GetIntListValue(this XElement node, char splitChar = ',') { if (node == null || string.IsNullOrEmpty(node.Value)) return new List(); if (string.IsNullOrEmpty(node.Value)) return new List(); return node.Value.Split(splitChar).Select(x => int.Parse(x)).ToList(); } public static Guid? GetGuidValue(this XElement node) { if (node == null || string.IsNullOrEmpty(node.Value)) return null; return Guid.Parse(node.Value); } public static List GetGuidListValue(this XElement node, char splitChar = ',') { if (node == null || string.IsNullOrEmpty(node.Value)) return default(List); return node.Value.Split(splitChar).Select(x => Guid.Parse(x)).ToList(); } public static double? GetDoubleValue(this XAttribute node) { if (node == null) return null; if (string.IsNullOrEmpty(node.Value)) return null; return double.Parse(node.Value); } public static DateTime? GetDateTimeValue(this XAttribute node) { if (node == null) return null; if (string.IsNullOrEmpty(node.Value)) return null; return DateTime.Parse(node.Value); } public static string GetTrimStringValue(this XAttribute node) { if (node == null) return string.Empty; return node.Value.Trim(); } public static string GetStringValue(this XAttribute node) { if (node == null) return string.Empty; return node.Value; } public static List GetStringListValue(this XAttribute node, char splitChar = ',') { var values = default(List); if (node != default(XAttribute)) { values = node.Value.Split(new char[] { splitChar }, StringSplitOptions.RemoveEmptyEntries).ToList(); } return values; } public static decimal? GetDecimalValue(this XAttribute node) { if (node == null || string.IsNullOrEmpty(node.Value)) return null; if (string.IsNullOrEmpty(node.Value)) return null; return decimal.Parse(node.Value); } public static int? GetIntValue(this XAttribute node) { if (node == null || string.IsNullOrEmpty(node.Value)) return null; if (string.IsNullOrEmpty(node.Value)) return null; return int.Parse(node.Value); } public static List GetIntListValue(this XAttribute node, char splitChar = ',') { if (node == null || string.IsNullOrEmpty(node.Value)) return new List(); if (string.IsNullOrEmpty(node.Value)) return new List(); return node.Value.Split(splitChar).Select(x => int.Parse(x)).ToList(); } public static Guid? GetGuidValue(this XAttribute node) { if (node == null || string.IsNullOrEmpty(node.Value)) return null; return Guid.Parse(node.Value); } public static List GetGuidListValue(this XAttribute node, char splitChar = ',') { if (node == null || string.IsNullOrEmpty(node.Value)) return default(List); return node.Value.Split(splitChar).Select(x => Guid.Parse(x)).ToList(); } } }