System.Xml.XmlNamespaceManager.GetNamespacesInScope C# (CSharp) Méthode

GetNamespacesInScope() public méthode

public GetNamespacesInScope ( XmlNamespaceScope scope ) : string>.IDictionary
scope XmlNamespaceScope
Résultat string>.IDictionary
        public virtual IDictionary<string,string> GetNamespacesInScope( XmlNamespaceScope scope ) {
#pragma warning restore 3002
            int i = 0;
            switch ( scope ) {
                case XmlNamespaceScope.All:
                    i = 2;
                    break;
                case XmlNamespaceScope.ExcludeXml:
                    i = 3;
                    break;
                case XmlNamespaceScope.Local:
                    i = lastDecl;
                    while ( nsdecls[i].scopeId == scopeId ) {
                        i--;
                        Debug.Assert( i >= 2 );
                    }
                    i++;
                    break;
            }

            Dictionary<string,string> dict = new Dictionary<string, string>( lastDecl - i + 1 );
            for( ; i <= lastDecl; i++ ) {
                string prefix = nsdecls[i].prefix;
                string uri = nsdecls[i].uri;
                Debug.Assert( prefix != null );

                if ( uri != null ) {
                    if ( uri.Length > 0 || prefix.Length > 0 || scope == XmlNamespaceScope.Local ) {
                        dict[prefix] = uri;
                    }
                    else {
                        // default namespace redeclared to "" -> remove from list for all scopes other than local
                        dict.Remove( prefix );
                    }
                }
            }
            return dict;
        }

Usage Example

        public string GetValue(XElement element, XmlNamespaceManager nsm)
        {
            XPathContext context = new XPathContext((NameTable)nsm.NameTable);
            XPathNavigator navigator = element.CreateNavigator();
            object result = null;

            foreach (var ns in nsm.GetNamespacesInScope(XmlNamespaceScope.All))
                context.AddNamespace(ns.Key, ns.Value);

            context.Arguments.AddParam(XPathContext.ParameterNames.CurrentNode, string.Empty, navigator.Select("."));
            result = navigator.Evaluate(this.RawValue, context);

            if (result is string)
                return (string)result;
            else if (result is XPathNodeIterator)
            {
                var iterator = ((XPathNodeIterator)result);
                var current = (XPathNavigator)((IEnumerable)iterator).Cast<object>().First();

                return current.Value;
            }
            else if (result is XAttribute)
                return ((XAttribute)result).Value;
            else if (result is XElement)
                return ((XElement)result).Value;

            return string.Empty;
        }
All Usage Examples Of System.Xml.XmlNamespaceManager::GetNamespacesInScope