Question: I'm building an API for my app and would like to return errors in the XML response that are generated by validation errors.

So say you're registering on the site, right now the validation errors returned might be:

```
Login has already been taken
Password is too short (minimum is 6 characters)
Email has already been taken

```

But I'd like to reformat that as:

```
<errors>
    <error>Login has already been taken</error>
    <error>Password is too short (minimum is 6 characters)</error>
    <error>Email has already been taken</error>
</errors>

```

So, how would I access the validation error array to do that?

Answer: 
Rails gives you this functionality by default, i.e.

```
  user.errors.to_xml

```

Will give you

```
=>  <?xml version="1.0" encoding="UTF-8"?>
    <errors>  
     <error>Name can't be blank</error>
     <error>Wiki url can't be blank</error>
     <error>User can't be blank</error>
    </errors>

```

Rails 7

```
user.errors.to_hash.to_xml

```
Call #to\_xml on errors method

```

user = User.new
unless user.valid?
  return user.errors.to_xml
end

```