¿Hay alguna manera de acceder a un XmlReader de forma asíncrona? El xml viene de la red desde muchos clientes diferentes, como en XMPP; es un flujo constante de etiquetas <action>...</action
>.XmlReader asincrónico en .NET?
Lo que busco es poder utilizar una interfaz BeginRead/EndRead-like. La mejor solución que he logrado es hacer una lectura asíncrona de 0 bytes en la transmisión de red subyacente, y cuando llegan algunos datos, llame a Read en el XmlReader; sin embargo, esto bloqueará hasta que todos los datos del nodo se vuelve disponible. Esa solución se ve más o menos así EDITAR
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);
}
: Se trata de un posible algoritmo para la elaboración de si una cadena 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;
};
su contador falla en este caso, que es * perfectamente * legal XML: , donde el límite de lectura es anterior a baz. –