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

GetNamespacesInScope() public method

public GetNamespacesInScope ( XmlNamespaceScope scope ) : string>.IDictionary
scope XmlNamespaceScope
return string>.IDictionary
        public virtual IDictionary<string, string> GetNamespacesInScope(XmlNamespaceScope scope)
        {
            XPathNodeType nt = NodeType;
            if ((nt != XPathNodeType.Element && scope != XmlNamespaceScope.Local) || nt == XPathNodeType.Attribute || nt == XPathNodeType.Namespace)
            {
                XPathNavigator navSave = Clone();

                // If current item is not an element, then try parent
                if (navSave.MoveToParent())
                    return navSave.GetNamespacesInScope(scope);
            }

            Dictionary<string, string> dict = new Dictionary<string, string>();

            // "xml" prefix always in scope
            if (scope == XmlNamespaceScope.All)
                dict["xml"] = XmlReservedNs.NsXml;

            // Now add all in-scope namespaces
            if (MoveToFirstNamespace((XPathNamespaceScope)scope))
            {
                do
                {
                    string prefix = LocalName;
                    string ns = Value;

                    // Exclude xmlns="" declarations unless scope = Local
                    if (prefix.Length != 0 || ns.Length != 0 || scope == XmlNamespaceScope.Local)
                        dict[prefix] = ns;
                }
                while (MoveToNextNamespace((XPathNamespaceScope)scope));

                MoveToParent();
            }

            return dict;
        }

Usage Example

        public BindingExpressionContext(BaseParser parser, XPathNavigator boundNode, IDictionary<string, string> inScopeNamespaces)
        {
            this.Parser = parser;
             this.BoundNode = boundNode;

             if (inScopeNamespaces == null) {
            inScopeNamespaces = boundNode.GetNamespacesInScope(XmlNamespaceScope.All);
             }

             this._InScopeNamespaces = inScopeNamespaces;
        }
All Usage Examples Of System.Xml.XPath.XPathNavigator::GetNamespacesInScope