Note that there are some explanatory texts on larger screens.

plurals
  1. POAsynchronous XmlReader in .NET?
    text
    copied!<p>Is there a way to access a XmlReader asynchronously? The xml is coming in off the network from many different clients like in XMPP; it is a constant stream of <code>&lt;action&gt;...&lt;/action</code>> tags.</p> <p>What i'm after is to be able to use a BeginRead/EndRead-like interface. The best solution I've managed to come up with is to do an asynchronous read for 0 bytes on the underlying network stream, then when some data arrives, call Read on the XmlReader- this will however block until all of the data from the node becomes available. That solution looks roughly like this</p> <pre><code>private Stream syncstream; private NetworkStream ns; private XmlReader reader; //this code runs first public void Init() { syncstream = Stream.Synchronized(ns); reader = XmlReader.Create(syncstream); byte[] x = new byte[1]; syncstream.BeginRead(x, 0, 0, new AsynchronousCallback(ReadCallback), null); } private void ReadCallback(IAsyncResult ar) { syncstream.EndRead(ar); reader.Read(); //this will block for a while, until the entire node is available //do soemthing to the xml node byte[] x = new byte[1]; syncstream.BeginRead(x, 0, 0, new AsynchronousCallback(ReadCallback), null); } </code></pre> <p>EDIT: This is a possible algorithm for working out if a string contains a complete xml node?</p> <pre><code>Func&lt;string, bool&gt; nodeChecker = currentBuffer =&gt; { //if there is nothing, definetly no tag if (currentBuffer == "") return false; //if we have &lt;![CDATA[ and not ]]&gt;, hold on, else pass it on if (currentBuffer.Contains("&lt;![CDATA[") &amp;&amp; !currentBuffer.Contains("]]&gt;")) return false; if (currentBuffer.Contains("&lt;![CDATA[") &amp;&amp; currentBuffer.Contains("]]&gt;")) return true; //these tag-related things will also catch &lt;? ?&gt; processing instructions //if there is a &lt; but no &gt;, we still have an open tag if (currentBuffer.Contains("&lt;") &amp;&amp; !currentBuffer.Contains("&gt;")) return false; //if there is a &lt;...&gt;, we have a complete element. //&gt;...&lt; will never happen because we will pass it on to the parser when we get to &gt; if (currentBuffer.Contains("&lt;") &amp;&amp; currentBuffer.Contains("&gt;")) return true; //if there is no &lt; &gt;, we have a complete text node if (!currentBuffer.Contains("&lt;") &amp;&amp; !currentBuffer.Contains("&gt;")) return true; //&gt; and no &lt; will never happen, we will pass it on to the parser when we get to &gt; //by default, don't block return false; }; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload