如何读取大的XML文件,而不在内存中加载并使用的XElement
-
20-09-2019 - |
题
我想读一个大的xml文件(100 + M)。由于它的大小,我不想使用的XElement将其加载到内存中。我使用LINQ的XML查询分析和阅读。
什么是做到这一点的最好方法是什么?上的XPath的组合或的XmlReader使用LINQ XML /的XElement任何例子?
请帮忙。感谢。
解决方案
是的,你可以用结合的XmlReader方法XNode.ReadFrom 时,看到它使用C#,以选择性地处理由的XmlReader作为的XElement找到的节点的文档中的示例中
其他提示
MSDN文档的XNode.ReadFrom
方法中的示例代码如下:
class Program
{
static IEnumerable<XElement> StreamRootChildDoc(string uri)
{
using (XmlReader reader = XmlReader.Create(uri))
{
reader.MoveToContent();
// Parse the file and display each of the nodes.
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
if (reader.Name == "Child")
{
XElement el = XElement.ReadFrom(reader) as XElement;
if (el != null)
yield return el;
}
break;
}
}
}
}
static void Main(string[] args)
{
IEnumerable<string> grandChildData =
from el in StreamRootChildDoc("Source.xml")
where (int)el.Attribute("Key") > 1
select (string)el.Element("GrandChild");
foreach (string str in grandChildData)
Console.WriteLine(str);
}
}
但是我发现,在该实例中StreamRootChildDoc
方法需要如下进行修改:
static IEnumerable<XElement> StreamRootChildDoc(string uri)
{
using (XmlReader reader = XmlReader.Create(uri))
{
reader.MoveToContent();
// Parse the file and display each of the nodes.
while (!reader.EOF)
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "Child")
{
XElement el = XElement.ReadFrom(reader) as XElement;
if (el != null)
yield return el;
}
else
{
reader.Read();
}
}
}
}
请记住,你将需要阅读的文件按顺序并参照兄弟姐妹或后代的将是缓慢的,在最好的,也不可能在最坏的情况。否则@MartinHonnn有钥匙。
不隶属于 StackOverflow