System.Xml.Linq.XmlHelper.GetAttributeValue C# (CSharp) Méthode

GetAttributeValue() public méthode

获取第一个被xPath匹配到的元素的属性,如果属性不存在,返回""
public GetAttributeValue ( string xPath, string attributeName ) : string
xPath string xPath表达式
attributeName string 要获取的属性
Résultat string
        public string GetAttributeValue(string xPath, string attributeName)
        {
            var result = string.Empty;
            if (string.IsNullOrEmpty(attributeName)) return result;
            XElement element = _doc.XPathSelectElement(xPath);
            if (element == null) return result;
            if (!element.HasAttributes) return result;
            XAttribute attr = element.Attribute(attributeName);
            if (attr == null) return result;
            result = attr.Value;
            return result;
        }