private static List <string> GetGroups(NzbParserContext context, XContainer groupsElement) { return(groupsElement? .Elements(context.Namespace + NzbKeywords.Group) .Select(g => g.Value) .ToList()); }
public static XElement FindFirstElement(XContainer node, string localName) { var nodeElements = node?.Elements(); if (nodeElements == null && nodeElements.Count() == 0) { return(null); } foreach (var element in nodeElements) { if (element.Name.LocalName == localName) { return(element); } } foreach (var element in nodeElements) { var foundElement = FindFirstElement(element, localName); if (foundElement != null) { return(foundElement); } } return(null); }
private static List <NzbSegment> GetSegments(NzbParserContext context, XContainer segentsElement) { return(segentsElement? .Elements(context.Namespace + NzbKeywords.Segment) .Select(GetSegment) .ToList()); }
private static NntpGroups GetGroups(NzbParserContext context, XContainer groupsElement) { IEnumerable <string> groups = groupsElement? .Elements(context.Namespace + NzbKeywords.Group) .Select(g => g.Value); return(new NntpGroups(groups)); }
/// <summary> /// Returns whether a descendant of the specified <see cref="XElement" /> exists. /// </summary> /// <param name="element">The <see cref="XElement" />.</param> /// <param name="elementName">The name of the descendent node.</param> /// <param name="immediateOnly">Whether or not to search only the element's immediate descendants.</param> /// <returns><c>true</c> if the node exists or <c>false</c> otherwise.</returns> protected static bool Exists(XContainer element, string elementName, bool immediateOnly) { if (immediateOnly) { return(element?.Elements().Any(d => d.Name.LocalName.Equals(elementName, StringComparison.OrdinalIgnoreCase)) ?? false); } return(element?.Descendants().Any(d => d.Name.LocalName.Equals(elementName, StringComparison.OrdinalIgnoreCase)) ?? false); }
/// <summary> /// Gets XML configuration parameter value as string. /// </summary> /// <param name="parametersElement">Root container element with 'parameter' nodes.</param> /// <param name="parameterName">Name of the parameter.</param> /// <param name="defaultValue">Default value returned if the parameter name is not found.</param> /// <returns>Parameter value as string.</returns> private static string GetParameterValue(XContainer parametersElement, string parameterName, string defaultValue) { var parameterElement = parametersElement.Elements("parameter").FirstOrDefault(x => { var xAttribute = x.Attribute("name"); return(xAttribute != null && xAttribute.Value.ToLower().Equals(parameterName.ToLower())); }); return(parameterElement != null ? parameterElement.Value : defaultValue); }
/// <summary> /// Loads a structure layout based upon an XML container's children. /// </summary> /// <param name="layoutTag">The collection of structure field tags to parse.</param> /// <returns>The structure layout that was loaded.</returns> public static StructureLayout LoadLayout(XContainer layoutTag) { StructureLayout layout = new StructureLayout(); foreach (XElement element in layoutTag.Elements()) { HandleElement(layout, element); } return(layout); }
private static void DeleteItems(XContainer items) { foreach (var element in items.Elements()) { var database = Factory.GetDatabase(element.GetAttributeValue("database")); var obj = database?.GetItem(element.GetAttributeValue("id")); obj?.Delete(); } }
/// <summary> /// Deserializes any script writers defined in the specified <see cref="XContainer"/>. /// </summary> /// <param name="parent">The <see cref="XContainer"/> object containing the XML definitions for the script writers.</param> private void DeserializeScriptWriters(XContainer parent) { foreach (var element in parent.Elements(_hyperDbXmlNamespace + "add")) { _scriptWriterTypes.TryAdd( GetRequiredAttribute(element, "name"), Type.GetType(GetRequiredAttribute(element, "type"), true) // Deliberately throw an exception if we can't load the type ); } }
private static string GetElementValue(XContainer parent, string name) { var elem = parent.Elements(name).FirstOrDefault(); if (elem == null) { throw new Exception("Missing element " + name); } return(elem.Value); }
/// <summary> /// Adds the event to the collection. /// </summary> /// <param name="element">The event being added.</param> private void AddEvent(XContainer element) { foreach (XElement child in element.Elements()) { if (!this.Contains(child)) { this.Add(child); } } }
static void PrintDirectories(XContainer parent, string path) { foreach (XElement element in parent.Elements("dir")) { string dir = element.Attribute("name").Value; string fullPath = path == null ? dir : $"{path}/{dir}"; Console.WriteLine(fullPath); PrintDirectories(element, fullPath); } }
private static Dictionary <string, string> GetJoins(XContainer ele) { if (ele == null) { return(new Dictionary <string, string>()); } var ret = ele.Elements().ToDictionary(j => j.Attribute("name").Value, j => j.Value); return(ret); }
private static Dictionary <string, string> LoadKeyValueRule(XContainer doc, string section, string container) { IEnumerable <XElement> step1Pre = doc.Elements(section).Elements(container); return(step1Pre.Select(element => new { Key = element.Value.Split('|')[0], Value = element.Value.Split('|')[1] }) .GroupBy(arg => arg.Key, arg => arg.Value, (key, values) => new { Key = key, Value = values.First() }) .ToDictionary(arg => arg.Key, arg => arg.Value)); }
/// <summary> /// This method returns a list of elements matching the specified name, using the specified string comparison method. /// </summary> /// <param name="container">The XML container instance.</param> /// <param name="name">The name of the element to locate.</param> /// <param name="comparisonType">One of the System.StringComparison values.</param> /// <returns>List of elements matching the specified name.</returns> public static IEnumerable <XElement> Elements(this XContainer container, XName name, StringComparison comparisonType) { foreach (XElement element in container.Elements()) { if (String.Compare(element.Name.LocalName, name.LocalName, comparisonType) == 0) { yield return(element); } } }
/// <summary> /// Gets the first (in document order) child element with the specified local name and one of the namespaces. /// </summary> /// <param name="container">The node containing the child elements.</param> /// <param name="namespaces">A collection of namespaces used when searching for the child element.</param> /// <param name="localName">The local (unqualified) name to match.</param> /// <returns>A <see cref="XElement" /> that matches the specified name and namespace, or null.</returns> public static XElement Element(this XContainer container, IEnumerable <XNamespace> namespaces, string localName) { Contract.Requires(container != null); Contract.Requires(namespaces != null); Contract.Requires(!string.IsNullOrWhiteSpace(localName)); return(container.Elements() .FirstOrDefault( e => e.Name.LocalName == localName && namespaces.Contains(e.Name.Namespace))); }
private static IDictionary <string, object> LoadCommandParameters(XContainer xe) { return (xe .Elements(XName.Get("parameter", _menuDescriptorSchemaUri)) .ToDictionary( parameter => (string)parameter.Attribute(XName.Get("name")), parameter => (object)parameter.Value, StringComparer.OrdinalIgnoreCase) .AsReadOnly()); }
public static IEnumerable <XElement> ElementsIgnoreCase(this XContainer container, XName name) { foreach (var element in container.Elements()) { if (element.Name.NamespaceName == name.NamespaceName && string.Equals(element.Name.LocalName, name.LocalName, StringComparison.OrdinalIgnoreCase)) { yield return(element); } } }
private static IEnumerable <SimpleValueValidator> FindSimpleValueValidator(XContainer parent) { foreach (var simpleElement in parent.Elements("SimpleValue")) { var simple = new SimpleValueValidator(); simple.Id = simpleElement.Attribute("Id").Value; simple.Validation = simpleElement.Attribute("Validation").Value; yield return(simple); } }
public static XElement Element(this XContainer node, XName name, bool ignoreNamespace) { if (ignoreNamespace) { return(node.Elements().FirstOrDefault(e => e.Name.LocalName == name.LocalName)); } else { return(node.Element(name)); } }
// From https://stackoverflow.com/a/9335141/6651 public static IEnumerable <XElement> ElementsCaseInsensitive(XContainer source, XName name) { foreach (XElement e in source.Elements()) { if (e.Name.Namespace.NamespaceName.Equals(name.Namespace.NamespaceName, StringComparison.OrdinalIgnoreCase) && e.Name.LocalName.Equals(name.LocalName, StringComparison.OrdinalIgnoreCase)) { yield return(e); } } }
protected ConditionSet([NotNull] XContainer el) : this() { if (el == null) { throw new ArgumentNullException(nameof(el)); } foreach (XElement cel in el.Elements()) { Add(Parse(cel)); } }
private static XElement GetElement(XContainer element, XName name) { if (element == null) { throw new ArgumentNullException("element"); } var elems = element.Elements().Where(xElement => xElement.Name.Equals(name)); var xElements = elems as XElement[] ?? elems.ToArray(); return(xElements.Any() ? xElements.ElementAt(0) : null); }
/// <summary> /// Returns the element with the specified name, or creates and adds one if required. /// </summary> /// <param name="parent"></param> /// <param name="name"></param> /// <returns></returns> public static XElement ElementOrAdd(this XContainer parent, XName name, Func <XElement, bool> predicate) { var e = parent.Elements(name).FirstOrDefault(predicate); if (e == null) { parent.Add(e = new XElement(name)); } return(e); }
public static IEnumerable <XElement> FindElementSequences(this XContainer container, List <string> names) { foreach (XElement parent in container.Elements()) { XText child = parent.GetSequenceChild(names); if (child != null) { yield return(parent); } } }
// <summary> // Gets the first (in document order) child element with the specified local name and one of the namespaces. // </summary> // <param name="container">The node containing the child elements.</param> // <param name="namespaces">A collection of namespaces used when searching for the child element.</param> // <param name="localName">The local (unqualified) name to match.</param> // <returns>A <see cref="XElement" /> that matches the specified name and namespace, or null.</returns> public static XElement Element(this XContainer container, IEnumerable <XNamespace> namespaces, string localName) { DebugCheck.NotNull(container); DebugCheck.NotNull(namespaces); DebugCheck.NotEmpty(localName); return(container.Elements() .FirstOrDefault( e => e.Name.LocalName == localName && namespaces.Contains(e.Name.Namespace))); }
public static IEnumerable <XElement> GetRequiredElements ( [NotNull] this XContainer container, [NotNull] string elementName, bool ignoreNamespace = true ) { var elements = ignoreNamespace ? container.Elements().Where(a => a.Name.LocalName == elementName) : container.Elements(elementName); var requiredElements = elements.ToList(); if (!requiredElements.Any()) { throw new InvalidDataException($"Required element \"{elements}\" not found."); } return(requiredElements); }
//[Variation(Priority = 2, Desc = "XElement.AncestorsAndSelf(XName) I.", Param = true)] //[Variation(Priority = 2, Desc = "XElement.AncestorsAndSelf(XName) II.", Param = false)] #region Methods private void VerifyDeleteNodes <T>(IEnumerable <T> toRemove) where T : XNode { // copy of the data to delete IEnumerable <XNode> toRemoveCopy = toRemove.OfType <XNode>().ToList(); // Create array of parents IEnumerable <XContainer> parents = toRemove.Select(x => (x == null) ? (XContainer)null : (x.Parent != null ? (XContainer)x.Parent : (XContainer)x.Document)).ToList(); // calculate the expected results for the parents of the processed elements var expectedNodesForParent = new Dictionary <XContainer, List <ExpectedValue> >(); foreach (XContainer p in parents) { if (p != null) { expectedNodesForParent.TryAdd(p, p.Nodes().Except(toRemoveCopy.Where(x => x != null)).Select(a => new ExpectedValue(!(a is XText), a)).ProcessNodes().ToList()); } } toRemove.Remove(); IEnumerator <XNode> copyToRemove = toRemoveCopy.GetEnumerator(); IEnumerator <XContainer> parentsEnum = parents.GetEnumerator(); // verify on parents: deleted elements should not be found while (copyToRemove.MoveNext() && parentsEnum.MoveNext()) { XNode node = copyToRemove.Current; if (node != null) { XContainer parent = parentsEnum.Current; TestLog.Compare(node.Parent, null, "Parent of deleted"); TestLog.Compare(node.Document, null, "Document of deleted"); TestLog.Compare(node.NextNode, null, "NextNode of deleted"); TestLog.Compare(node.PreviousNode, null, "PreviousNode of deleted"); if (parent != null) { TestLog.Compare(parent.Nodes().Where(x => x == node).IsEmpty(), "Nodes axis"); if (node is XElement) { var e = node as XElement; e.Verify(); TestLog.Compare(parent.Element(e.Name) != node, "Element axis"); TestLog.Compare(parent.Elements(e.Name).Where(x => x == e).IsEmpty(), "Elements axis"); } // Compare the rest of the elements TestLog.Compare(expectedNodesForParent[parent].EqualAll(parent.Nodes(), XNode.EqualityComparer), "The rest of the nodes"); } } } }
private static DataRow AddRecord(DataTable rs, XContainer rsXml) { var values = new List <object>(); foreach (XElement fieldNode in rsXml.Elements()) { AddColumn(rs, fieldNode.Name.LocalName); values.Add(fieldNode.Value); } return(rs.Rows.Add(values.ToArray())); }
private string PrepareBookTitle(XContainer info) { XElement element = info.Elements(_ns + "book-title").FirstOrDefault(); if (element == null) { return(string.Empty); } return(element.Value); }