public static string GetValue(this XContainer element, string path, string defaultValue = null) { XElement xe = element.GetNode(path); if (xe != null) { return(xe.Value); } return(defaultValue); }
/// <summary>Verify the element node exists.</summary> /// <param name="container">The theme container.</param> /// <param name="elementPath">The element path.</param> /// <returns>The <see cref="bool" />.</returns> public static bool NodeExists(XContainer container, string elementPath) { if (container == null) { throw new ArgumentNullException(nameof(container)); } if (string.IsNullOrEmpty(elementPath)) { throw new ArgumentNullException(nameof(elementPath)); } XElement node = container.GetNode(elementPath); bool nodeExists = node != null; return(nodeExists); }
/// <summary>Retrieves the node from the path.</summary> /// <param name="container">The container.</param> /// <param name="path">The full node path.</param> /// <returns>The <see cref="XElement" />.</returns> public static XElement[] GetNodes(this XContainer container, string path) { path = path.Trim().Trim('/'); if ((container == null) || string.IsNullOrEmpty(path)) { return(null); } int _index = path.LastIndexOf('/'); if (_index <= -1) { return(null); } string _leftPath = path.Left(_index); string _lastPath = path.RemoveLeft(_index + 1); XElement lastNode = container.GetNode(_leftPath); return(lastNode?.Elements(_lastPath).Where(x => x != null).ToArray()); }
/// <summary>Reads the xml container element to string.</summary> /// <param name="container">The xml container.</param> /// <param name="elementPath">The element path.</param> /// <returns>The <see cref="string" />.</returns> public static string ReadElement(XContainer container, string elementPath) { if (container == null) { throw new ArgumentNullException(nameof(container)); } if (string.IsNullOrEmpty(elementPath)) { throw new ArgumentNullException(nameof(elementPath)); } string element; if (NodeExists(container, elementPath)) { if (NodeEmpty(container, elementPath)) { // Empty node return a default or null value; element = null; } else { // TODO: Create method to deserialize various color input data. Like Red. Currently only support HTML code (hex). // Read node value. element = container.GetNode(elementPath).Value; } } else { element = null; // Node not found logging to trace listener. Logger.WriteDebug($@"Unable to read the xml node. Path: {elementPath}"); } return(element); }
public static XElement[] GetNodes(this XContainer element, string path) { path = path.Trim().Trim('/'); if (element != null && !string.IsNullOrEmpty(path)) { int index = path.LastIndexOf('/'); if (index > -1) { string leftPath = path.Left(index); string lastPath = path.RemoveLeft(index + 1); XElement lastNode = element.GetNode(leftPath); if (lastNode != null) { return(lastNode.Elements(lastPath).Where(x => x != null).ToArray()); } } } return(null); }
/// <summary>Retrieves the value.</summary> /// <param name="element">The element.</param> /// <param name="path">The full path.</param> /// <param name="defaultValue">The default value.</param> /// <returns>The <see cref="string" />.</returns> public static string GetValue(this XContainer element, string path, string defaultValue = null) { return(element.GetNode(path)?.Value ?? defaultValue); }