Có cách nào để truy cập XmlReader không đồng bộ không? Các xml là đến tắt mạng từ nhiều khách hàng khác nhau như trong XMPP; nó là một dòng liên tục của <action>...</action
> thẻ.XmlReader không đồng bộ trong .NET?
Tôi đang theo dõi để có thể sử dụng giao diện BeginRead/EndRead. Giải pháp tốt nhất mà tôi đã tạo ra là thực hiện đọc không đồng bộ cho 0 byte trên luồng mạng cơ bản, sau đó khi một số dữ liệu đến, hãy đọc Đọc trên XmlReader- tuy nhiên điều này sẽ chặn cho đến khi tất cả dữ liệu từ nút có sẵn. Giải pháp đó trông giống như thế này
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: Đây là một thuật toán có thể làm việc nếu chuỗi chứa nút xml hoàn chỉnh?
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;
};
bộ đếm của bạn không thành công trong trường hợp này, là * hoàn toàn * hợp pháp XML: , trong đó ranh giới đọc là trước khi baz. –