public static IEnumerable<XAttribute> Attributes(this IEnumerable<XElement> elements, XName name, bool ignoreNamespace) { if (ignoreNamespace) return elements.Attributes().Where(a => a.Name.LocalName == name.LocalName); else return elements.Attributes(name); }
public static string GenerateXPathFromXElement(this XElement xUi) { var original = xUi.GetAttributeValue("XPath"); if (!String.IsNullOrEmpty(original)) return original; var tag = "//*"; var xTag = xUi.Attribute("tag"); var xType = xUi.Attribute("type"); if (xType != null) { if (xType.Value.Equals("a")) { tag = "//a"; xType.Remove(); } if(xType.Value.Equals("table")||xType.Value.Equals("div")) { var textAttr = xUi.Attribute("text"); if(textAttr!=null) textAttr.Remove(); } } if (xTag != null) { tag = "//" + xTag.Value; xTag.Remove(); } var xpath = tag; if (xUi.Attributes().Any()) { xpath = xpath + "["; var count = 0; foreach (XAttribute xa in xUi.Attributes()) { var key = xa.Name.ToString(); if (key.StartsWith("_")) continue; var value = xa.Value; if (count > 0) xpath = xpath + " and "; if (key.Equals("text")) { if (value.Length < 32) xpath = xpath + key + "()='" + value + "' "; else { xpath = xpath + "contains(text(),'" + value.Substring(0, 16) + "') "; } } else xpath = xpath + "@" + key + "='" + value + "' "; count++; } xpath = xpath + "]"; } return xpath; }
internal static Size GetSize(this XElement e) { var wAttr = e.Attributes().FirstOrDefault(a => a.Name.LocalName.ToLower() == "width"); var hAttr = e.Attributes().FirstOrDefault(a => a.Name.LocalName.ToLower() == "height"); var w = wAttr != null ? wAttr.Value : "0"; var h = hAttr != null ? hAttr.Value : "0"; var Size = new Size(int.Parse(w), int.Parse(h)); return Size; }
internal static Point GetLocation(this XElement e) { var xAttr = e.Attributes().FirstOrDefault(a => a.Name.LocalName.ToLower() == "x"); var yAttr = e.Attributes().FirstOrDefault(a => a.Name.LocalName.ToLower() == "y"); var x = xAttr != null ? xAttr.Value : "0"; var y = yAttr != null ? yAttr.Value : "0"; var Location = new Point(int.Parse(x), int.Parse(y)); return Location; }
public static string GetAttributeValue(this XElement element, string attributeName) { string result = null; if (element.HasAttributes && element.Attributes().Any(x => x.Name.LocalName == attributeName)) { result = element.Attributes().First(x => x.Name.LocalName == attributeName).Value; } return result; }
/// <summary> /// Returns a filtered collection of the attributes of every element in the source /// collection. Only elements that have a matching <see cref="System.Xml.Linq.XName"/> are /// included in the collection. /// </summary> /// <param name="elements"></param> /// <param name="name"></param> /// <param name="ignoreCase"></param> /// <returns></returns> public static IEnumerable<XAttribute> Attributes( this IEnumerable<XElement> elements, XName name, bool ignoreCase ) { if ( ignoreCase ) { return elements.Attributes().Where( p => p.Name.ToString().ToLower() == name.ToString().ToLower() ); } else { return elements.Attributes( name ); } }
internal static XElement RemoveAllNamespaces(this XElement e) { return new XElement(e.Name.LocalName, (from n in e.Nodes() select ((n is XElement) ? RemoveAllNamespaces(n as XElement) : n)), (e.HasAttributes) ? (from a in e.Attributes() where (!a.IsNamespaceDeclaration) && (a.Name.NamespaceName == "" // Doesn't check primary attribute || e.Attributes().All(x => x == a || a.Name.LocalName != x.Name.LocalName)) select new XAttribute(a.Name.LocalName, a.Value)) : null); }
public static string GetClassName(this XElement element) { XNamespace ns = "http://www.w3.org/1999/xhtml"; var noClass = element.Attributes().All(a => a.Name != (ns + "class")); return noClass ? string.Empty : element.Attribute("class").Value; }
/// <summary> /// Returns a filtered collection of attributes of this element. Only elements /// that have a matching System.Xml.Linq.XName are included in the collection, ignoring case. /// </summary> /// <param name="element">The class being extended.</param> /// <param name="name">The name of the attribute</param> /// <returns>XAttribute</returns> public static XAttribute AttributeIgnoreCase(this XElement element, XName name) { return element.Attributes() .FirstOrDefault( s => string.Equals(s.Name.LocalName, name.LocalName, StringComparison.OrdinalIgnoreCase)); }
public static IDictionary<string, object> ToValues(this XElement element) { var values = new Dictionary<string, object>(); foreach (var attribute in element.Attributes()) values[attribute.Name.ToString()] = attribute.Value; return values; }
// REVIEW: We can use a stack if the perf is bad for Except and MergeWith public static XElement Except(this XElement source, XElement target) { if (target == null) { return source; } IEnumerable<XAttribute> attributesToRemove = from e in source.Attributes() where AttributeEquals(e, target.Attribute(e.Name)) select e; // Remove the attributes foreach (XAttribute a in attributesToRemove.ToList()) { a.Remove(); } foreach (XElement sourceChild in source.Elements().ToList()) { XElement targetChild = FindElement(target, sourceChild); if (targetChild != null && !HasConflict(sourceChild, targetChild)) { Except(sourceChild, targetChild); bool hasContent = sourceChild.HasAttributes || sourceChild.HasElements; if (!hasContent) { // Remove the element if there is no content sourceChild.Remove(); targetChild.Remove(); } } } return source; }
public static void ShouldEqual(this XElement actual, XElement expected) { if (actual == null) { if (expected == null) { return; } throw new EqualException(expected, null); } actual.Name.ShouldEqual(expected.Name); actual.Attributes().ShouldEqual(expected.Attributes()); if (expected.HasElements != actual.HasElements) { throw new EqualException(expected, actual); } if (actual.HasElements) { actual.Elements().ShouldEqual(expected.Elements()); } else { if (actual.Value != expected.Value) { throw new EqualException(expected, actual); } } }
/// <summary> /// An XElement extension method that removes all namespaces described by @this. /// </summary> /// <param name="this">The @this to act on.</param> /// <returns>An XElement.</returns> public static XElement RemoveAllNamespaces(this XElement @this) { return new XElement(@this.Name.LocalName, (from n in @this.Nodes() select ((n is XElement) ? RemoveAllNamespaces(n as XElement) : n)), (@this.HasAttributes) ? (from a in @this.Attributes() select a) : null); }
public static XAttribute TryGetAttribute(this XElement element, XName attributeName) { var attribute = element.Attribute(attributeName) ?? element.Attributes().SingleOrDefault( a => a.Name.ToString().Homogenize() == attributeName.ToString().Homogenize()); return attribute ?? new XAttribute(attributeName, string.Empty); }
public static string DescribeSelector(this XElement e) { var me = e.Name.ToString(); if (e.HasAttributes) me += "[" + e.Attributes().Describe() + "]"; return me; }
/// <summary> /// Gets the condition attribute for a given <see cref="XElement"/>. /// </summary> /// <param name="element">The <see cref="XElement"/> that we want to get the attribute from.</param> /// <returns>The <see cref="XAttribute"/> if present, null otherwise.</returns> public static XAttribute GetConditionAttribute(this XElement element) { Contract.Requires(element != null); return element.Attributes() .FirstOrDefault(a => a.Name.LocalName == "Condition"); }
public static BsonDocument ToBsonDocument(this XElement xElement) { var bsonDocument = new BsonDocument(); var type = xElement.Name.ToString(); bsonDocument.Add(Constants._TYPE, type); var mainId = xElement.Attribute(Constants._ID) != null ? xElement.Attribute(Constants._ID).Value : null; foreach (XAttribute attribure in xElement.Attributes()) { var key = attribure.Name.ToString(); var value = attribure.Value; if (bsonDocument.Contains(key)) bsonDocument.Set(key, value); else bsonDocument.Add(key, value); } foreach (XElement kid in xElement.Descendants()) { var id = kid.Attribute(Constants._ID); if (id == null || mainId == null) { bsonDocument.Add(kid.Name.ToString(), kid.ToBsonDocument()); } else { kid.SetAttributeValue(Constants.PARENT_ID, mainId); DBFactory.GetData().Save(kid); } } return bsonDocument; }
public static string GetXAttribute(this XElement element, string xAttribute) { return (from attribute in element.Attributes() where attribute.Name.LocalName.Equals(xAttribute) select attribute.Value).FirstOrDefault(); }
public static XAttribute RemoveAttributeCI(this XElement x, string attributeName) { var attr = x.Attributes().Where(a => a.Name.LocalName.CaseInsensitiveCompare(attributeName)).FirstOrDefault(); if(attr != null) attr.Remove(); return attr; }
public static string TryGetAttributeValue(this XElement element, XName attributeName) { var attribute = element.Attribute(attributeName) ?? element.Attributes().SingleOrDefault( a => a.Name.ToString().Homogenize() == attributeName.ToString().Homogenize()); return attribute == null ? null : attribute.Value; }
public static IEnumerable<XmlAttribute> Attributes(this XmlElement source, Func<XmlAttribute, bool> predicate) { Throw.ArgumentNullException.IfNull(source, nameof(source)); Throw.ArgumentNullException.IfNull(predicate, nameof(predicate)); return source.Attributes().Where(predicate); }
public static IEnumerable<string> GetMetaAttributes(this HtmlNode root, params string[] names) { return from name in names let xpath = new XpathBuilder().Any("meta").WithAttribute("name", name) from attribute in root.Attributes(xpath.ToString()) where "content".Equals(attribute.Name, StringComparison.InvariantCultureIgnoreCase) select attribute.Value.Safe(); }
/// <summary> /// Returns the attribute of the element with the specified local name. /// </summary> /// <param name="element">The parent element.</param> /// <param name="prefix">The namespace prefix</param> /// <param name="localName">The local name.</param> /// <returns>The attribute with the specified local name, or null if no attribute matches the name.</returns> public static XAttribute Attribute(this XElement element, string prefix, string localName) { IEnumerator<XAttribute> enumerator = element.Attributes().Where(e => prefix != null ? (e.Name.LocalName == localName) && (e.Name.Namespace == element.GetNamespaceOfPrefix(prefix)) : e.Name.LocalName == localName).GetEnumerator(); return enumerator.MoveNext() ? enumerator.Current : null; }
internal static IEnumerable<XAttribute> AllAttributes(this XElement element) { return element.Attributes().Select(each => { if (string.IsNullOrWhiteSpace(each.Name.NamespaceName)) { return new XAttribute(element.Name.Namespace + each.Name.LocalName, each.Value); } return each; }); }
public static string AttributeValue(this XElement element, string attributeName) { if (element == null) { return null; } var attr = element.Attributes().FirstOrDefault(each => each.Name == attributeName); return attr != null ? attr.Value : null; }
public static void WithoutAttribute(this XElement element, string name) { // namespaces schamespaces XAttribute result = element.Attributes().Where(x => string.Equals(name, x.Name.LocalName, StringComparison.CurrentCultureIgnoreCase)). FirstOrDefault(); Assert.That(result, Is.Null, string.Format("Element {1} should not have attribute matching name {0}", name, element)); }
public static XAttribute GetAttribute(this XElement value, string attributeName, string pmid) { var attribute = value.Attributes(attributeName).FirstOrDefault(); if (attribute == null) throw new Exceptions.PubMedAttributeNotFoundException($"Attribute {attributeName} for pubmed id {pmid} is null"); return attribute; }
public static void SetAttributeCI(this XElement x, string attributeName, object value) { var attr = x.Attributes().Where(a => a.Name.LocalName.CaseInsensitiveCompare(attributeName)).FirstOrDefault(); if(attr != null && value != null) attr.SetValue(value); else if(attr == null) x.SetAttributeValue(attributeName, value); }
/// <summary> /// Returns a new XElement which is a carbon copy of the provided element, /// but with no child nodes. Useful for writing exception messages without /// inadvertently disclosing secret key material. It is assumed that the /// element name itself and its attribute values are not secret. /// </summary> public static XElement WithoutChildNodes(this XElement element) { var newElement = new XElement(element.Name); foreach (var attr in element.Attributes()) { newElement.SetAttributeValue(attr.Name, attr.Value); } return newElement; }
public static decimal GetAttribute(this XElement root, string name, decimal defaultValue) { string strValue = (string)root.Attributes(name).FirstOrDefault() ?? defaultValue.ToString(); decimal value; if (!decimal.TryParse(strValue, out value)) { throw new Exception(string.Format("Attribute {0}: Value retrieved was not a valid decimal", name)); } return value; }