System.Xml.XPath.XPathNavigator.MoveToAttribute C# (CSharp) Method

MoveToAttribute() public method

public MoveToAttribute ( string localName, string namespaceURI ) : bool
localName string
namespaceURI string
return bool
        public virtual bool MoveToAttribute(string localName, string namespaceURI)
        {
            if (MoveToFirstAttribute())
            {
                do
                {
                    if (localName == LocalName && namespaceURI == NamespaceURI)
                        return true;
                }
                while (MoveToNextAttribute());

                MoveToParent();
            }

            return false;
        }

Usage Example

        public override string GetAttribute(string localName, string namespaceURI)
        {
            if (null == localName)
            {
                throw new ArgumentNullException("localName");
            }
            // reader allows calling GetAttribute, even when positioned inside attributes
            XPathNavigator nav = _nav;

            switch (nav.NodeType)
            {
            case XPathNodeType.Element:
                break;

            case XPathNodeType.Attribute:
                nav = nav.Clone();
                if (!nav.MoveToParent())
                {
                    return(null);
                }
                break;

            default:
                return(null);
            }
            // are they really looking for a namespace-decl?
            if (namespaceURI == XmlConst.ReservedNsXmlNs)
            {
                if (localName == "xmlns")
                {
                    localName = string.Empty;
                }
                return(nav.GetNamespace(localName));
            }
            if (null == namespaceURI)
            {
                namespaceURI = string.Empty;
            }
            // We need to clone the navigator and move the clone to the attribute to see whether the attribute exists,
            // because XPathNavigator.GetAttribute return string.Empty for both when the the attribute is not there or when
            // it has an empty value. XmlReader.GetAttribute must return null if the attribute does not exist.
            if ((object)nav == (object)_nav)
            {
                nav = nav.Clone();
            }
            if (nav.MoveToAttribute(localName, namespaceURI))
            {
                return(nav.Value);
            }
            else
            {
                return(null);
            }
        }
All Usage Examples Of System.Xml.XPath.XPathNavigator::MoveToAttribute