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

HasNamespace() public méthode

public HasNamespace ( string prefix ) : bool
prefix string
Résultat bool
        public virtual bool HasNamespace( string prefix ) {
            // Don't assume that prefix is atomized
            for( int thisDecl = lastDecl; nsdecls[thisDecl].scopeId == scopeId; thisDecl-- ) {
                if ( String.Equals( nsdecls[thisDecl].prefix, prefix ) && nsdecls[thisDecl].uri != null ) {
                    if ( prefix.Length > 0 || nsdecls[thisDecl].uri.Length > 0 ) {
                        return true;
                    }
                    return false;
                }
            }
            return false;
        }

Usage Example

Exemple #1
0
        /// <summary>
        /// Creates a <see cref="XmlNamespaceManager"/> for <paramref name="document"/>.
        /// Namespaces declared in the document node are automatically added.
        /// The default namespace is given the prefix 'ns'.
        /// </summary>
        public static XmlNamespaceManager CreateNamespaceManager(this XmlDocument document)
        {
            var manager = new XmlNamespaceManager(document.NameTable);

            foreach (XmlNode node in document.SelectNodes("//node()"))
            {
                if (node is XmlElement)
                {
                    var element = node as XmlElement;

                    foreach (XmlAttribute attribute in element.Attributes)
                    {
                        if (attribute.Name == "xmlns")
                        {
                            // The first default namespace wins
                            // (since using multiple default namespaces in a single file is not considered a good practice)
                            if (!manager.HasNamespace("ns"))
                            {
                                manager.AddNamespace("ns", attribute.Value);
                            }
                        }

                        if (attribute.Prefix == "xmlns")
                        {
                            manager.AddNamespace(attribute.LocalName, attribute.Value);
                        }
                    }
                }
            }

            return manager;
        }
All Usage Examples Of System.Xml.XmlNamespaceManager::HasNamespace