Ok, I solved the problem reading the position of <div id="foo"> from the &binvar, copying some bytes from that position in the file in another &binvar and cutting the &binvar where I find the </div>.
Maybe is a little complicated but it seems to work fine.