System.Xml.Xsl.Runtime.XmlQueryOutput.WriteNamespaceDeclaration C# (CSharp) Method

WriteNamespaceDeclaration() public method

Before calling XmlRawWriter.WriteNamespaceDeclaration(), perform various checks to ensure well-formedness.
public WriteNamespaceDeclaration ( string prefix, string ns ) : void
prefix string
ns string
return void
        public void WriteNamespaceDeclaration(string prefix, string ns) {
            string nsExisting;
            Debug.Assert(prefix != null && ns != null);

            ConstructInEnumAttrs(XPathNodeType.Namespace);

            if (this.nsmgr == null) {
                // If namespace manager has not yet been created, then there is no possibility of conflict
                WriteNamespaceDeclarationUnchecked(prefix, ns);
            }
            else {
                nsExisting = this.nsmgr.LookupNamespace(prefix);
                if (ns != nsExisting) {
                    // prefix = "", ns = "", nsExisting --> Look for xmlns="", found xmlns="foo"
                    // prefix = "", ns, nsExisting = null --> Look for xmlns="uri", no uri found
                    // prefix = "", ns, nsExisting = "" --> Look for xmlns="uri", found xmlns=""
                    // prefix = "", ns, nsExisting --> Look for xmlns="uri", found xmlns="uri2"
                    // prefix, ns, nsExisting = null --> Look for xmlns:foo="uri", no uri found
                    // prefix, ns, nsExisting --> Look for xmlns:foo="uri", found xmlns:foo="uri2"

                    // If the prefix is mapped to a uri,
                    if (nsExisting != null) {
                        // Then throw an error except in a special case involving default namespace declaration
                        // Looking for xmlns="", found xmlns="foo" -- ERROR
                        // Looking for xmlns="uri", found xmlns="uri2" -- ERROR
                        // Looking for xmlns="uri", found xmlns="" -- OK, unless xmlns="" declaration is
                        //                                            local or element's namespace is ""
                        if (prefix.Length != 0 || nsExisting.Length != 0 || this.useDefNmsp)
                            throw new XslTransformException(Res.XmlIl_NmspConflict, new string[] {prefix.Length == 0 ? "" : ":", prefix, ns, nsExisting});
                    }

                    // Add namespace to manager and write it to output
                    AddNamespace(prefix, ns);
                }
            }

            if (this.depth == 0)
                EndTree();
        }