Get attribute value and convert to other type
using System; using System.Linq; using System.Xml.Linq; namespace DACU.Tools { static class XmlUtils { public static bool? NodeValueToBool(XElement node) { if (node == null) return null; return node.Value == "1" ? (bool?)true : (node.Value == "0" ? (bool?)false : null); } public static int[] NodeValueToArray(XElement node) { if (node == null || node.IsEmpty) return null; var ints = node.Elements("lid"); return ints == null ? null : ints.Where(elem => !String.IsNullOrWhiteSpace(elem.Value)).Select(elem => Convert.ToInt32(elem.Value)).ToArray(); } public static string GetStringFromAttribute(this XElement element, string attributeName) { XAttribute att = element.Attribute(attributeName); return att == null || att.Value == null ? "" : att.Value; } public static int GetIntFromAttribute(this XElement element, string attributeName) { XAttribute att = element.Attribute(attributeName); if (att == null) return -1; int value; if (int.TryParse(att.Value, out value)) return value; return -1; } public static double GetDoubleFromAttribute(this XElement element, string attributeName) { XAttribute att = element.Attribute(attributeName); if (att == null) return double.NaN; double value; if (double.TryParse(att.Value, out value)) return value; return double.NaN; } public static float GetFloatFromAttribute(this XElement element, string attributeName) { XAttribute att = element.Attribute(attributeName); if (att == null) return float.NaN; float value; if (float.TryParse(att.Value, out value)) return value; return float.NaN; } public static bool GetBoolFromAttribute(this XElement element, string attributeName) { XAttribute att = element.Attribute(attributeName); if (att == null) return false; bool value; if (bool.TryParse(att.Value, out value)) return value; return false; } } }