def to_xml_updates
      doc = Nokogiri::XML::Document.new
      root = doc.create_element("Updates")
      self.class.elements.each do |name, field|
        value = read_attribute(name)
        # Create or update existing fields
        if changes.include?(name.to_s)
          field.to_xml_updates(value) do |field_uri_xml, element_xml|
            # Exchange does not like updating to nil, so delete those.
            if element_xml.text.present?
              set_item_field = doc.create_element("SetItemField")
              set_item_field << field_uri_xml
              element_wrapper = doc.create_element(tag_name)
              element_wrapper << element_xml
              set_item_field << element_wrapper
              root << set_item_field
            else
              delete_item_field = doc.create_element("DeleteItemField")
              delete_item_field << field_uri_xml
              root << delete_item_field
            end
          end
        end
        # Delete removed phone numbers, etc
        if changes.include?(name.to_s) && value.is_a?(Array)
          old_values, new_values = changes[name.to_s]
          deleted_values = old_values - new_values
          field.to_xml_updates(deleted_values) do |field_uri_xml, _|
            delete_item_field = doc.create_element("DeleteItemField")
            delete_item_field << field_uri_xml
            root << delete_item_field
          end
        end
      end
      root
    end
def to_xml_updates
      doc = Nokogiri::XML::Document.new
      root = doc.create_element("Updates")
      self.class.elements.each do |name, field|
        value = read_attribute(name)
        # Create or update existing fields
        if changes.include?(name.to_s)
          field.to_xml_updates(value) do |field_uri_xml, element_xml|
            # Exchange does not like updating to nil, so delete those.
            if element_xml.text.present?
              set_item_field = doc.create_element("SetItemField")
              set_item_field << field_uri_xml
              element_wrapper = doc.create_element(tag_name)
              element_wrapper << element_xml
              set_item_field << element_wrapper
              root << set_item_field
            else
              delete_item_field = doc.create_element("DeleteItemField")
              delete_item_field << field_uri_xml
              root << delete_item_field
            end
          end
        end
        # Delete removed phone numbers, etc
        if changes.include?(name.to_s) && value.is_a?(Array)
          old_values, new_values = changes[name.to_s]
          deleted_values = old_values - new_values
          field.to_xml_updates(deleted_values) do |field_uri_xml, _|
            delete_item_field = doc.create_element("DeleteItemField")
            delete_item_field << field_uri_xml
            root << delete_item_field
          end
        end
      end
      root
    end
Builds XML Set/Delete fields for update operations.