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

RemapPrefix() private method

Remaps an element or attribute prefix using the following rules: 1. If another in-scope prefix is already mapped to "ns", then use that 2. Otherwise, if a prefix was previously mapped to "ns" by this method, then use that 3. Otherwise, generate a new prefix of the form 'xp_??', where ?? is a stringized counter These rules tend to reduce the number of unique prefixes used throughout the tree.
private RemapPrefix ( string prefix, string ns, bool isElemPrefix ) : string
prefix string
ns string
isElemPrefix bool
return string
        private string RemapPrefix(string prefix, string ns, bool isElemPrefix) {
            const string PrefixFormat = "xp_{0}";
            string genPrefix;
            Debug.Assert(prefix != null && ns != null && ns.Length != 0);

            if (this.conflictPrefixes == null)
                this.conflictPrefixes = new Hashtable(16);

            if (this.nsmgr == null) {
                this.nsmgr = new XmlNamespaceManager(this.runtime.NameTable);
                this.nsmgr.PushScope();
            }

            // Rule #1: If another in-scope prefix is already mapped to "ns", then use that
            genPrefix = this.nsmgr.LookupPrefix(ns);
            if (genPrefix != null) {
                // Can't use an empty prefix for an attribute
                if (isElemPrefix || genPrefix.Length != 0)
                    goto ReturnPrefix;
            }

            // Rule #2: Otherwise, if a prefix was previously mapped to "ns" by this method, then use that
            // Make sure that any previous prefix is different than "prefix"
            genPrefix = this.conflictPrefixes[ns] as string;
            if (genPrefix != null && genPrefix != prefix) {
                // Can't use an empty prefix for an attribute
                if (isElemPrefix || genPrefix.Length != 0)
                    goto ReturnPrefix;
            }

            // Rule #3: Otherwise, generate a new prefix of the form 'xp_??', where ?? is a stringized counter
            genPrefix = string.Format(CultureInfo.InvariantCulture, PrefixFormat, this.prefixIndex++);

        ReturnPrefix:
            // Save generated prefix so that it can be possibly be reused later
            this.conflictPrefixes[ns] = genPrefix;

            return genPrefix;
        }