本文整理汇总了C#中this.Attributes方法的典型用法代码示例。如果您正苦于以下问题:C# this.Attributes方法的具体用法?C# this.Attributes怎么用?C# this.Attributes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类this
的用法示例。
在下文中一共展示了this.Attributes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Attributes
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);
}
示例2: GenerateXPathFromXElement
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;
}
示例3: GetSize
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;
}
示例4: GetLocation
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;
}
示例5: GetAttributeValue
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;
}
示例6: Attributes
/// <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 );
}
}
示例7: RemoveAllNamespaces
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);
}
示例8: GetClassName
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;
}
示例9: AttributeIgnoreCase
/// <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));
}
示例10: ToValues
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;
}
示例11: Except
// 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;
}
示例12: ShouldEqual
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);
}
}
}
示例13: RemoveAllNamespaces
/// <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);
}
示例14: TryGetAttribute
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);
}
示例15: DescribeSelector
public static string DescribeSelector(this XElement e)
{
var me = e.Name.ToString();
if (e.HasAttributes)
me += "[" + e.Attributes().Describe() + "]";
return me;
}