How to set the text format of a field in a node object? When I create a node like this:

$nodeSetup = [
  'nid' =&gt; NULL,
  'type' =&gt; $bundle,
  'uid' =&gt; $uid,
  'revision' =&gt; 0,
  'status' =&gt; 1,
  'body' =&gt; ['format' =&gt; 'full_html', 'value' =&gt; Xss::filter($body)],
];
$_node = Node::create($nodeSetup);


the body value and format work fine. But, when I load an existing node, how can I set the body value keeping the format? This didnt work:

$_node-&gt;body-&gt;format = 'full_html';
$_node-&gt;body-&gt;set('format', 'full_html');
$_node-&gt;save();
The following worked for me,

$node = \Drupal::entityTypeManager()-&gt;getStorage('node')-&gt;load($nid);
$node-&gt;body-&gt;value = 'body';
$node-&gt;body-&gt;format = 'full_html';//omitting this will retain existing format
$node-&gt;save();