Advertisement
Guest User

XML extension methods

a guest
Mar 4th, 2014
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.37 KB | None | 0 0
  1. using System.Globalization;
  2. using System.Xml.Linq;
  3.  
  4. namespace Demo.Utility
  5. {
  6.     public static class XmlUtilites
  7.     {
  8.         public static string ElementValue(this XElement element, string childName, string defaultValue = null)
  9.         {
  10.             if (element == null) return defaultValue;
  11.  
  12.             var child = element.Element(childName);
  13.             return child != null ? child.Value : defaultValue;
  14.         }
  15.  
  16.         public static double ElementDoubleValue(this XElement element, string childName, CultureInfo cultureInfo = null)
  17.         {
  18.             if (element == null) return defaultValue;
  19.  
  20.             var culture = cultureInfo ?? CultureInfo.InvariantCulture;
  21.             var child = element.Element(childName);
  22.             return child != null && !string.IsNullOrEmpty(child.Value) ? double.Parse(child.Value, culture) : double.NaN;            
  23.         }
  24.  
  25.         public static decimal ElementDecimalValue(this XElement element, string childName, CultureInfo cultureInfo = null)
  26.         {
  27.             if (element == null) return defaultValue;
  28.  
  29.             var child = element.Element(childName);
  30.             var culture = cultureInfo ?? CultureInfo.InvariantCulture;
  31.             return child != null && !string.IsNullOrEmpty(child.Value)
  32.                        ? decimal.Parse(child.Value, culture)
  33.                        : 0;
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement