private string NextPrefix()
 {
     if (_usedPrefixes == null)
     {
         return(_aliasBase + (++_tempNamespacePrefix));
     }
     while (_usedPrefixes.ContainsKey(++_tempNamespacePrefix))
     {
         ;
     }
     return(_aliasBase + _tempNamespacePrefix);
 }
        private InternalHashtable ListUsedPrefixes(InternalHashtable nsList, string prefix)
        {
            InternalHashtable qnIndexes = new InternalHashtable();
            int          prefixLength   = prefix.Length;
            const string MaxInt32       = "2147483647";

            foreach (string alias in _namespaces.Namespaces.Keys)
            {
                string name;
                if (alias.Length > prefixLength)
                {
                    name = alias;
                    if (name.Length > prefixLength && name.Length <= prefixLength + MaxInt32.Length && name.StartsWith(prefix, StringComparison.Ordinal))
                    {
                        bool numeric = true;
                        for (int j = prefixLength; j < name.Length; j++)
                        {
                            if (!Char.IsDigit(name, j))
                            {
                                numeric = false;
                                break;
                            }
                        }
                        if (numeric)
                        {
                            Int64 index = Int64.Parse(name.Substring(prefixLength), NumberStyles.Integer, CultureInfo.InvariantCulture);
                            if (index <= Int32.MaxValue)
                            {
                                Int32 newIndex = (Int32)index;
                                if (!qnIndexes.ContainsKey(newIndex))
                                {
                                    qnIndexes.Add(newIndex, newIndex);
                                }
                            }
                        }
                    }
                }
            }
            if (qnIndexes.Count > 0)
            {
                return(qnIndexes);
            }
            return(null);
        }
        /// <include file='doc\XmlSerializationWriter.uex' path='docs/doc[@for="XmlSerializationWriter.WriteStartElement5"]/*' />
        protected void WriteStartElement(string name, string ns, object o, bool writePrefixed, XmlSerializerNamespaces xmlns)
        {
            if (o != null && _objectsInUse != null)
            {
                if (_objectsInUse.ContainsKey(o))
                {
                    throw new InvalidOperationException(SR.Format(SR.XmlCircularReference, o.GetType().FullName));
                }
                _objectsInUse.Add(o, o);
            }

            string prefix = null;
            bool   needEmptyDefaultNamespace = false;

            if (_namespaces != null)
            {
                foreach (string alias in _namespaces.Namespaces.Keys)
                {
                    string aliasNs = (string)_namespaces.Namespaces[alias];

                    if (alias.Length > 0 && aliasNs == ns)
                    {
                        prefix = alias;
                    }
                    if (alias.Length == 0)
                    {
                        if (aliasNs == null || aliasNs.Length == 0)
                        {
                            needEmptyDefaultNamespace = true;
                        }
                        if (ns != aliasNs)
                        {
                            writePrefixed = true;
                        }
                    }
                }
                _usedPrefixes = ListUsedPrefixes(_namespaces.Namespaces, _aliasBase);
            }
            if (writePrefixed && prefix == null && ns != null && ns.Length > 0)
            {
                prefix = _w.LookupPrefix(ns);
                if (prefix == null || prefix.Length == 0)
                {
                    prefix = NextPrefix();
                }
            }
            if (prefix == null && xmlns != null)
            {
                prefix = xmlns.LookupPrefix(ns);
            }
            if (needEmptyDefaultNamespace && prefix == null && ns != null && ns.Length != 0)
            {
                prefix = NextPrefix();
            }
            _w.WriteStartElement(prefix, name, ns);
            if (_namespaces != null)
            {
                foreach (string alias in _namespaces.Namespaces.Keys)
                {
                    string aliasNs = (string)_namespaces.Namespaces[alias];
                    if (alias.Length == 0 && (aliasNs == null || aliasNs.Length == 0))
                    {
                        continue;
                    }
                    if (aliasNs == null || aliasNs.Length == 0)
                    {
                        if (alias.Length > 0)
                        {
                            throw new InvalidOperationException(SR.Format(SR.XmlInvalidXmlns, alias));
                        }
                        WriteAttribute("xmlns", alias, null, aliasNs);
                    }
                    else
                    {
                        if (_w.LookupPrefix(aliasNs) == null)
                        {
                            // write the default namespace declaration only if we have not written it already, over wise we just ignore one provided by the user
                            if (prefix == null && alias.Length == 0)
                            {
                                break;
                            }
                            WriteAttribute("xmlns", alias, null, aliasNs);
                        }
                    }
                }
            }
            WriteNamespaceDeclarations(xmlns);
        }
 private InternalHashtable ListUsedPrefixes(InternalHashtable nsList, string prefix)
 {
     InternalHashtable qnIndexes = new InternalHashtable();
     int prefixLength = prefix.Length;
     const string MaxInt32 = "2147483647";
     foreach (string alias in _namespaces.Namespaces.Keys)
     {
         string name;
         if (alias.Length > prefixLength)
         {
             name = alias;
             if (name.Length > prefixLength && name.Length <= prefixLength + MaxInt32.Length && name.StartsWith(prefix, StringComparison.Ordinal))
             {
                 bool numeric = true;
                 for (int j = prefixLength; j < name.Length; j++)
                 {
                     if (!Char.IsDigit(name, j))
                     {
                         numeric = false;
                         break;
                     }
                 }
                 if (numeric)
                 {
                     Int64 index = Int64.Parse(name.Substring(prefixLength), NumberStyles.Integer, CultureInfo.InvariantCulture);
                     if (index <= Int32.MaxValue)
                     {
                         Int32 newIndex = (Int32)index;
                         if (!qnIndexes.ContainsKey(newIndex))
                         {
                             qnIndexes.Add(newIndex, newIndex);
                         }
                     }
                 }
             }
         }
     }
     if (qnIndexes.Count > 0)
     {
         return qnIndexes;
     }
     return null;
 }