# File lib/qbxml/request.rb, line 130
    def to_xml(as_set=true)
      return (RequestSet.new(self)).to_xml if as_set # Simple call yields xml as a single request in a request set. However, if the xml for the lone request is required, pass false.
      req = Builder::XmlMarkup.new(:indent => 2)
      request_root, container = case
      when @type.is_one_of?(:query)
        ["#{@klass.class_leaf_name}QueryRq", nil]
      when @type == :add
        ["#{@klass.class_leaf_name}AddRq", "#{@klass.class_leaf_name}Add"]
      when @type == :mod
        ["#{@klass.class_leaf_name}ModRq", "#{@klass.class_leaf_name}Mod"]
      when @type == :delete
        ["#{@klass.ListOrTxn}DelRq", nil]
      when @type == :deleted
        ["#{@klass.ListOrTxn}DeletedQueryRq", nil]
      else
        raise RuntimeError, "Could not convert this request to qbxml!\n#{self.inspect}"
      end
      inner_stuff = lambda {
        deep_tag = lambda {|k,v|
          if v.is_a?(Hash)
            if k == ''
              v.each { |k,v|
                deep_tag.call(k,v)
              }
            else
              req.tag!(k.camelize) { v.each { |k,v| deep_tag.call(k,v) } }
            end
          else
            req.tag!(k.camelize,uncast(v))
          end
        }

        # Add the specific elements for the respective request type
        if @type.is_one_of?(:add, :mod)
          if @type == :mod
            # First the ObjectId:
            req.tag!(@klass.ListOrTxn + 'ID', @object.send("#{@klass.ListOrTxn}Id".underscore))
            # Second the EditSequence
            req.tag!('EditSequence', @object.send(:edit_sequence))
          end
          # Then, all the dirty_attributes
          deep_tag.call('',@object.to_dirty_hash) # (this is an hash statically ordered to the model's qbxml attribute order)
        elsif @type == :query && @object.class == @klass
          # Sent an instance object for a query - we should include the ListId/TxnId (then other filters?)
          req.tag!(@klass.ListOrTxn + 'ID', @object.send("#{@klass.ListOrTxn}Id".underscore))
          deep_tag.call('', @filters)
        elsif @type == :delete
          req.tag!(@klass.ListOrTxn + 'DelType', @klass.class_leaf_name)
          req.tag!(@klass.ListOrTxn + 'ID', @object.send("#{@klass.ListOrTxn}Id".underscore))
        else
          # just filters
          deep_tag.call('', @filters)
        end
        # Lastly, specify the fields to return, if desired
        @ret_elements.each { |r| req.tag!('IncludeRetElement', r) } if @ret_elements.is_a?(Array) && @ret_elements.length > 0
      }
      req.tag!(request_root, :requestID => self.class.next_request_id) {
        if container
          req.tag!(container) {
            inner_stuff.call
          }
        else
          inner_stuff.call
        end
      }
      # puts req.target!
      req.target!
    end