//----------------------------------
        // private method
        //----------------------------------

        /// <summary>
        /// Serialize the tree out to the stream.
        /// </summary>
        private void SerializeImp(
            BamlLocalizer           localizer,
            BamlTree                tree, 
            Stream                  output
            )
        {

            Debug.Assert(output != null, "The output stream given is null");
            Debug.Assert(tree != null && tree.Root != null, "The tree to be serialized is null.");

            _writer = new BamlWriter(output);            
            _bamlTreeStack = new Stack<BamlTreeNode>();
        
            // intialize the stack.
            _bamlTreeStack.Push(tree.Root);

            while (_bamlTreeStack.Count > 0)
            {
                BamlTreeNode currentNode = _bamlTreeStack.Pop();
                if (!currentNode.Visited)
                {           
                    // Mark this node so that it won't be serialized again.
                    currentNode.Visited = true; 
                    currentNode.Serialize(_writer);            
                    PushChildrenToStack(currentNode.Children);                                   
                }
                else
                {
                    BamlStartElementNode elementNode = currentNode as BamlStartElementNode;
                    Debug.Assert(elementNode != null);                    

                    if (elementNode != null)
                    {                   
                        localizer.RaiseErrorNotifyEvent(
                            new BamlLocalizerErrorNotifyEventArgs(
                                BamlTreeMap.GetKey(elementNode),
                                BamlLocalizerError.DuplicateElement
                            )
                        );
                    }                                     
                }
            }
            
            // do not close stream as we don't own it.            
        }
 internal override void Serialize(BamlWriter writer)
 {
     writer.WriteRoutedEvent(
         _assemblyName, 
         _ownerTypeFullName, 
         _eventIdName, 
         _handlerName
         );
 }
 internal override void Serialize(BamlWriter writer)
 {
     writer.WriteText(_content, _typeConverterAssemblyName, _typeConverterName);
 }
 internal override void Serialize(BamlWriter writer)
 {
     writer.WriteLiteralContent(_literalContent);
 }
 internal override void Serialize(BamlWriter writer)
 {
     // skip seralizing Localization.Comments and Localization.Attributes properties
     if (!LocComments.IsLocCommentsProperty(_ownerTypeFullName, _propertyName)
      && !LocComments.IsLocLocalizabilityProperty(_ownerTypeFullName, _propertyName))            
     {
         writer.WriteProperty(
          _assemblyName, 
          _ownerTypeFullName, 
          _propertyName, 
          _value, 
          _attributeUsage
          );
     }             
 }
 internal override void Serialize(BamlWriter writer)
 {
     writer.WriteEndComplexProperty();
 }
 internal override void Serialize(BamlWriter writer)
 {
     writer.WriteContentProperty(
             _assemblyName,
             _typeFullName,
             _propertyName
             );
 }
 internal override void Serialize(BamlWriter writer)
 {
     writer.WritePIMapping(
         _xmlNamespace, 
         _clrNamespace, 
         _assemblyName
         );                
 }
 internal override void Serialize(BamlWriter writer)
 {
     writer.WriteEndElement();
 }
 internal override void Serialize(BamlWriter writer)
 {
     writer.WriteStartElement(_assemblyName, _typeFullName, _isInjected, _useTypeConverter);
 }
 internal override void Serialize(BamlWriter writer)
 {
     writer.WriteConnectionId(_connectionId);
 }
 internal override void Serialize(BamlWriter writer)
 {
     writer.WriteStartDocument();
 }
 /// <summary>
 /// Serialize the node through BamlWriter
 /// </summary>
 internal abstract void Serialize(BamlWriter writer);
 internal override void Serialize(BamlWriter writer)
 {
     writer.WritePresentationOptionsAttribute(_name, _value);
 }
 internal override void Serialize(BamlWriter writer)
 {
     writer.WriteEvent(
         _eventName, 
         _handlerName
         );
 }
 internal override void Serialize(BamlWriter writer)
 {
     writer.WriteDefAttribute(_name, _value);
 }
 internal override void Serialize(BamlWriter writer)
 {
     writer.WriteXmlnsProperty(_prefix, _xmlns);
 }
 internal override void Serialize(BamlWriter writer)
 {
     writer.WriteStartComplexProperty(
         _assemblyName, 
         _ownerTypeFullName, 
         _propertyName
         );
 }
 internal override void Serialize(BamlWriter writer)
 {
     writer.WriteEndConstructor();
 }