How can I generate custom XML for a SOAP client?
This is how I've done it:

some-feed.html.twig

&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"&gt;
  &lt;channel&gt;
    &lt;atom:link href="{{ some_variable_set_in_preprocess }}" rel="self" type="application/rss+xml"/&gt;
      /* -- rest of feed omitted */
  &lt;/channel&gt;
&lt;/rss&gt;


Then hook_theme() to register the template:

function hook_theme() {
  return [
    'some_feed' =&gt; [
      'render element' =&gt; 'elements',
      'template' =&gt; 'some-feed',
    ],
  ];
}


You can then generate a response with that theme:

$build = [
  '#theme' =&gt; 'some_feed',
];

$response = new \Symfony\Component\HttpFoundation\Response();
$xml = \Drupal::service('renderer')-&gt;renderRoot($build);
$response-&gt;setContent($xml);
$response-&gt;headers-&gt;set('Content-Type', 'text/xml');


You now have a Response object to work with. Depending on where you are working, you can return that, or set it as a response.