/// <summary>
 /// Writes the specified start tag and associates it with the given namespace and prefix.
 /// Inherited from <c>XmlTextWriter</c>, see <see cref="XmlTextWriter.WriteStartElement"/>
 /// Overridden to detect <c>exsl:document</c> element start tag.
 /// </summary>
 /// <param name="prefix">The namespace prefix of the element.</param>
 /// <param name="localName">The local name of the element.</param>
 /// <param name="ns">The namespace URI to associate with the element. If this namespace
 /// is already in scope and has an associated prefix then the writer will automatically write that prefix also. </param>
 /// <exception cref="InvalidOperationException">The writer is closed.</exception>
 public override void WriteStartElement(string prefix, string localName, string ns)
 {
     CheckContentStart();
     //Is it exsl:document redirecting instruction?
     if (ns == RedirectNamespace && localName == RedirectElementName)
     {
         //Lazy stack of states
         if (states == null)
         {
             states = new Stack();
         }
         //If we are redirecting already - push the current state into the stack
         if (redirectState == RedirectState.Redirecting)
         {
             states.Push(state);
         }
         //Initialize new state
         state         = new OutputState();
         redirectState = RedirectState.WritingRedirectElementAttrs;
     }
     else
     {
         if (redirectState == RedirectState.Redirecting)
         {
             if (state.Method == OutputMethod.Text)
             {
                 state.Depth++;
                 return;
             }
             //Write doctype before the first element
             if (state.Depth == 0 && state.SystemDoctype != null)
             {
                 if (prefix != String.Empty)
                 {
                     state.XmlWriter.WriteDocType(prefix + ":" + localName,
                                                  state.PublicDoctype, state.SystemDoctype, null);
                 }
                 else
                 {
                     state.XmlWriter.WriteDocType(localName,
                                                  state.PublicDoctype, state.SystemDoctype, null);
                 }
             }
             state.XmlWriter.WriteStartElement(prefix, localName, ns);
             state.Depth++;
         }
         else
         {
             base.WriteStartElement(prefix, localName, ns);
         }
     }
 }
 /// <summary>
 /// Finishes output redirecting - closes current writer
 /// and pops previous state.
 /// </summary>
 internal void FinishRedirecting()
 {
     state.CloseWriter();
     //Pop previous state if it exists
     if (states.Count != 0)
     {
         state         = (OutputState)states.Pop();
         redirectState = RedirectState.Redirecting;
     }
     else
     {
         state         = null;
         redirectState = RedirectState.Relaying;
     }
 }
 /// <summary>
 /// Finishes output redirecting - closes current writer 
 /// and pops previous state.
 /// </summary>
 internal void FinishRedirecting()
 {
     state.CloseWriter();
     //Pop previous state if it exists
     if (states.Count != 0) {
         state = (OutputState)states.Pop();
         redirectState = RedirectState.Redirecting;
     } else {
         state = null;
         redirectState = RedirectState.Relaying;
     }
 }
 /// <summary>
 /// Writes the specified start tag and associates it with the given namespace and prefix.
 /// Inherited from <c>XmlTextWriter</c>, see <see cref="XmlTextWriter.WriteStartElement"/>
 /// Overridden to detect <c>exsl:document</c> element start tag.
 /// </summary>        
 /// <param name="prefix">The namespace prefix of the element.</param>
 /// <param name="localName">The local name of the element.</param>
 /// <param name="ns">The namespace URI to associate with the element. If this namespace 
 /// is already in scope and has an associated prefix then the writer will automatically write that prefix also. </param>
 /// <exception cref="InvalidOperationException">The writer is closed.</exception>
 public override void WriteStartElement(string prefix, string localName, string ns)
 {
     CheckContentStart();
     //Is it exsl:document redirecting instruction?
     if (ns == RedirectNamespace && localName == RedirectElementName) {
         //Lazy stack of states
         if (states == null)
             states = new Stack();
         //If we are redirecting already - push the current state into the stack
         if (redirectState == RedirectState.Redirecting)
             states.Push(state);
         //Initialize new state
         state = new OutputState();
         redirectState = RedirectState.WritingRedirectElementAttrs;
     } else {
         if (redirectState == RedirectState.Redirecting) {
             if (state.Method == OutputMethod.Text) {
                 state.Depth++;
                 return;
             }
             //Write doctype before the first element
             if (state.Depth == 0 && state.SystemDoctype != null)
                 if (prefix != String.Empty)
                     state.XmlWriter.WriteDocType(prefix+":"+localName,
                         state.PublicDoctype,state.SystemDoctype, null);
                 else
                     state.XmlWriter.WriteDocType(localName,
                         state.PublicDoctype,state.SystemDoctype, null);
             state.XmlWriter.WriteStartElement(prefix, localName, ns);
             state.Depth++;
         } else
             base.WriteStartElement(prefix, localName, ns);
     }
 }