8

我想读取一个大的 xml 文件(100+M)。由于它的大小,我不想使用 XElement 将它加载到内存中。我正在使用 linq-xml 查询来解析和读取它。

最好的方法是什么?关于 XPath 或 XmlReader 与 linq-xml/XElement 组合的任何示例?

请帮忙。谢谢。

4

3 回答 3

9

是的,您可以将 XmlReader 与方法 XNode.ReadFrom结合使用,请参阅文档中的示例,该示例使用 C# 选择性地将 XmlReader 找到的节点作为 XElement 处理。

于 2010-02-12T14:14:38.377 回答
6

XNode.ReadFrom方法的 MSDN 文档中的示例代码如下:

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();
                }
            }
        }
    }
于 2013-08-16T20:59:12.770 回答
1

请记住,您必须按顺序读取文件,并且引用兄弟姐妹或后代将是最好的,最坏的情况是不可能的。否则@MartinHonnn 有钥匙。

于 2010-02-12T14:29:23.010 回答