C'è un modo per accedere a XmlReader in modo asincrono? L'xml sta uscendo dalla rete da molti client diversi come in XMPP; è un flusso costante di tag <action>...</action
>.Asynchronous XmlReader in .NET?
Quello che voglio è poter utilizzare un'interfaccia BeginRead/EndRead-like. La soluzione migliore che ho trovato è quella di eseguire una lettura asincrona per 0 byte sul flusso di rete sottostante, quindi quando arrivano alcuni dati, chiama Read su XmlReader- questo tuttavia bloccherà fino a tutti i dati dal nodo diventa disponibile. Tale soluzione si presenta più o meno come questo
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);
}
EDIT: Questo è un possibile algoritmo per lavorare fuori se una stringa contiene un nodo XML completo?
Func<string, bool> nodeChecker = currentBuffer =>
{
//if there is nothing, definetly no tag
if (currentBuffer == "") return false;
//if we have <![CDATA[ and not ]]>, hold on, else pass it on
if (currentBuffer.Contains("<![CDATA[") && !currentBuffer.Contains("]]>")) return false;
if (currentBuffer.Contains("<![CDATA[") && currentBuffer.Contains("]]>")) return true;
//these tag-related things will also catch <? ?> processing instructions
//if there is a < but no >, we still have an open tag
if (currentBuffer.Contains("<") && !currentBuffer.Contains(">")) return false;
//if there is a <...>, we have a complete element.
//>...< will never happen because we will pass it on to the parser when we get to >
if (currentBuffer.Contains("<") && currentBuffer.Contains(">")) return true;
//if there is no < >, we have a complete text node
if (!currentBuffer.Contains("<") && !currentBuffer.Contains(">")) return true;
//> and no < will never happen, we will pass it on to the parser when we get to >
//by default, don't block
return false;
};
il contatore non funziona in questo caso, che è * perfettamente * XML legale: , dove il limite di lettura è precedente a baz. –