Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何读取大型xml文件而不将其加载到内存中并使用XElement_Xml_Xpath_Linq To Xml_Large Files_Xelement - Fatal编程技术网

如何读取大型xml文件而不将其加载到内存中并使用XElement

如何读取大型xml文件而不将其加载到内存中并使用XElement,xml,xpath,linq-to-xml,large-files,xelement,Xml,Xpath,Linq To Xml,Large Files,Xelement,我想读取一个大的xml文件(100+M)。由于其大小,我不想使用XElement将其加载到内存中。我使用LINQXML查询来解析和读取它 最好的方法是什么?有关于XPath或XmlReader与LINQXML/XElement结合的例子吗 请帮忙。谢谢。是的,您可以将XmlReader与结合在一起,请参阅文档中的示例,该示例使用C#选择性地将XmlReader找到的节点作为元素进行处理。请记住,您必须按顺序读取文件,引用同级或子级的速度最好是缓慢的,最坏是不可能的。否则@MartinHonnn拥

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

最好的方法是什么?有关于XPath或XmlReader与LINQXML/XElement结合的例子吗


请帮忙。谢谢。

是的,您可以将XmlReader与结合在一起,请参阅文档中的示例,该示例使用C#选择性地将XmlReader找到的节点作为元素进行处理。

请记住,您必须按顺序读取文件,引用同级或子级的速度最好是缓慢的,最坏是不可能的。否则@MartinHonnn拥有密钥。

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);
    }
}
    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();
                }
            }
        }
    }

明亮的我正在开发一个应用程序,它将处理多个2亿XML文件,而XDocument让我心烦意乱。这已经取得了巨大的进步。谢谢。我认为
XNode.ReadFrom
文档页面上的示例代码中有一个bug。语句
XElement el=XElement.ReadFrom(reader)作为XElement
应该是
XElement el=new-XElement(reader.Name,reader.Value)取而代之。按原样,在它从中读取的XML文件中,每两个“Child”元素中的第一个被跳过;请参阅,以了解为什么这两个“read”方法不应混合使用。[Jon的回答没有明确提到
XNode.ReadFrom
方法,但我相信同样的问题也适用。]是的。第一个例子不起作用。它会读得太多,并且会跳过每一个“孩子”