【发布时间】:2013-12-21 20:24:12
【问题描述】:
我有一个 API,可以从(数百万条记录)中提取大型数据集。
我因此使用XMLReader:
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;
string xmldocstring = "";
using (XmlReader reader = XmlReader.Create(uri, settings))
{
reader.ReadStartElement("DATASET");
while (!reader.EOF)
{
if (reader.NodeType == XmlNodeType.Element)
{
try
{
XElement elR = XNode.ReadFrom(reader) as XElement;
//PROCESS XML AND DO WHATEVER WITH IT
}
catch (Exception ex)
{
Log.WriteLine(ex.Message);
Log.WriteLine(ex.StackTrace);
}
}
else
{
reader.Read();
}
}
}
它大部分时间都有效。但是,如果我得到一个超过 150 万条记录的记录集,我会始终收到以下错误,该错误记录在我的日志中(由上面代码中的 catch 记录)。
错误消息:无法从传输连接读取数据:连接已关闭。
堆栈跟踪:在 System.Net.ConnectStream.Read(Byte[] 缓冲区,Int32 偏移量,Int32 大小)
在 System.Xml.XmlRegisteredNonCachedStream.Read(Byte[] 缓冲区,Int32 偏移量,Int32 计数)
在 System.Xml.XmlTextReaderImpl.ReadData()
在 System.Xml.XmlTextReaderImpl.ParseText(Int32& startPos, Int32& endPos, Int32& outOrChars)
在 System.Xml.XmlTextReaderImpl.ParseText()
在 System.Xml.XmlTextReaderImpl.ParseElementContent()
在 System.Xml.XmlTextReaderImpl.Read()
在 System.Xml.XmlReader.ReadStartElement(字符串名称)
在 My_Files.DataBuilder.GetDataFromAPI(Dictionary`2 pData, String uri, String type, String siteid, String mlid, String mid, DateTime start, DateTime end)
在 My_Files.DataBuilder.GetTransactionData(Dictionary`2 pData, String type, String siteID, String mlid, String mid, String apiurl, String apipass, DateTime start, DateTime end)
在 My_Files.Program.GetAndProcessData(String transactionFile, Dictionary
2 pData, String siteid, String mlid, List1 dmids, DateTime dStartDate, DateTime dEndDate)在 My_Files.Program.Run()
发生了什么事?如何拉取大数据集的 xml 数据?
【问题讨论】:
-
可能是it is a timeout?
-
是的,它可能是。 1. 我怎么知道。 2. 如果是,我该如何解决?
-
我想您可以尝试使用链接问题中接受的答案进行连接。如果它有效,它可能会超时。除了链接问题中指定的方式之外,我不确定如何解决它。
-
看起来这个答案是同时拉回所有数据。是对的吗? GetResponse 获取整个响应?这会对我造成内存不足异常,因为数据通常大于 .net 应用程序的 2GB 限制。
-
好点。我错过了您的代码不会撤回所有内容的事实。