How to read large xml file without loading it in memory and using XElement

后端 未结 3 1527

I want to read a large xml file (100+M). Due to its size, I do not want to load it in memory using XElement. I am using linq-xml queries to parse and read it.

What\

3条回答
  •  情深已故
    2020-11-27 22:30

    The example code in the MSDN documentation for the XNode.ReadFrom method is as follows:

    class Program
    {
        static IEnumerable 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 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);
        }
    }
    

    But I've found that the StreamRootChildDoc method in the example needs to be modified as follows:

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

提交回复
热议问题