System.Xml.XmlTextWriter.PushNamespace C# (CSharp) Method

PushNamespace() private method

private PushNamespace ( string prefix, string ns, bool declared ) : void
prefix string
ns string
declared bool
return void
        void PushNamespace(string prefix, string ns, bool declared) {
            if (XmlReservedNs.NsXmlNs == ns) {
                throw new ArgumentException(Res.GetString(Res.Xml_CanNotBindToReservedNamespace));
            }

            if (prefix == null) {
                switch (stack[top].defaultNsState) {
                    case NamespaceState.DeclaredButNotWrittenOut:
                        Debug.Assert (declared == true, "Unexpected situation!!");
                        // the first namespace that the user gave us is what we
                        // like to keep. 
                        break;
                    case NamespaceState.Uninitialized:
                    case NamespaceState.NotDeclaredButInScope:
                        // we now got a brand new namespace that we need to remember
                        stack[top].defaultNs = ns;
                        break;
                    default:
                        Debug.Assert(false, "Should have never come here");
                        return;
                }
                stack[top].defaultNsState = (declared ? NamespaceState.DeclaredAndWrittenOut : NamespaceState.DeclaredButNotWrittenOut);
            }
            else {
                if (prefix.Length != 0 && ns.Length == 0) {
                    throw new ArgumentException(Res.GetString(Res.Xml_PrefixForEmptyNs));
                }

                int existingNsIndex = LookupNamespace(prefix);
                if (existingNsIndex != -1 && nsStack[existingNsIndex].ns == ns) {
                    // it is already in scope.
                    if (declared) {
                        nsStack[existingNsIndex].declared = true;
                    }
                }
                else {
                    // see if prefix conflicts for the current element
                    if (declared) {
                        if (existingNsIndex != -1 && existingNsIndex > stack[top].prevNsTop) {
                            nsStack[existingNsIndex].declared = true; // old one is silenced now
                        }
                    }
                    AddNamespace(prefix, ns, declared);
                }
            }
        }