我有一个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大小)
at System.Xml.XmlRegisteredNonCachedStream.Read(Byte [] buffer,Int32 offset,Int32 count)
在System.Xml.XmlTextReaderImpl.ReadData()
在System.Xml.XmlTextReaderImpl.ParseText(Int32& startPos,Int32& endPos,Int32& outOrChars)
在System.Xml.XmlTextReaderImpl.ParseText()
at System.Xml.XmlTextReaderImpl.ParseElementContent()
在System.Xml.XmlTextReaderImpl.Read()
在System.Xml.XmlReader.ReadStartElement(String name)
at My_Files.DataBuilder.GetDataFromAPI(Dictionary`2 pData,String uri,String type,String siteid,String mlid,String mid,DateTime start,DateTime end)
at My_Files.DataBuilder.GetTransactionData(Dictionary`2 pData,String type,String siteID,String mlid,String mid,String apiurl,String apipass,DateTime start,DateTime end)
at My_Files.Program.GetAndProcessData(String transactionFile,Dictionary
2 pData, String siteid, String mlid, List
1 dmids,DateTime dStartDate,DateTime dEndDate)at My_Files.Program.Run()
发生了什么事?如何获取大型xml数据集?
答案 0 :(得分:0)
看来,被调用进程需要很长时间才能生成XML文件。
您没有说明uri的价值。在被调用进程开始返回数据之前,许多Web服务器都有一个短暂的超时。如果要在服务器上构建整个文档,可能需要很长时间。
仅仅因为您使用阅读器传输结果,并不意味着服务器正在传输数据。
我会把注意力集中在被叫过程上。