I’m using an XmlWriter to send an XMPP (Jingle) stream. The XMPP protocol replaces the an XML stream when it negotiates TLS which means the XML ends up like:
<stream>
<features>
...
</features>
...TLS gets negotiated...
<stream>
...
</stream>
The XML end up being not well formed because there are two stream start tags.
What I want to do is throw away the XmlWriter I am using prior to the TLS negotiation and create a brand new one, it allows me to better modularise my code. However when I call myXmlWriter.Close() to make sure it gets disposed it will send the closing stream end element which breaks the XMPP protocol.
Is there anyway I can close the XmlWriter without it sending the outstanding end element?
Create an intermediate stream which you can use to disconnect the XmlWriter from the base stream.
This is not the most elegant solution, and the code below needs work, so test it before you put this into production, but it’s about the idea.
This class is meant to be used as a stream between the
XmlWriterand the stream theXmlWriteris outputting to. This class simply forwards all calls from theXmlWriterto the base stream, but once you callDisconnectBaseStream, it stops forwarding them and theXmlWritercannot control the base stream anymore.You can use this class like this:
Again, the
DummyStreamis a starting point and will need some work. You will for example want to make sure that theXmlWriterisn’t making calls after the disconnect that will crash, so you will want to to some checking with e.g. theWritemethod whetherBaseStreamisnulland if yes, just skip the call.