I'm using `libxml/xmlwriter` to generate an XML file within a program. 

```
const char *s = someCharactersFromSomewhere();
xmlTextWriterWriteAttribute (writer, _xml ("value"), _xml (s));

```

In general I don't have much control over the contents of `s`, so I can't guarantee that it will be well-formatted in UTF-8. Mostly it is, but if not, the XML which is generated will be malformed.

What I'd like to find is a way to convert `s` to valid UTF-8, with any invalid character sequences in `s` replaced with escapes or removed.

Alternatively, if there is an alternative to `xmlTextWriterWriteAttribute`, or some option I can pass in when initializing the XML writer, such that it guarantees that it will always write valid UTF-8, that would be even better.

One more thing to mention is that the solution must work with both Linux and OSX. Ideally writing as little of my own code as possible! :P