{"id":843,"date":"2010-06-07T13:04:00","date_gmt":"2010-06-07T13:04:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vbteam\/2010\/06\/07\/vb-xml-cookbook-recipe-7-enumerating-large-xml-files-doug-rothaus\/"},"modified":"2024-07-05T12:41:10","modified_gmt":"2024-07-05T19:41:10","slug":"vb-xml-cookbook-recipe-7-enumerating-large-xml-files-doug-rothaus","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/vbteam\/vb-xml-cookbook-recipe-7-enumerating-large-xml-files-doug-rothaus\/","title":{"rendered":"VB XML Cookbook, Recipe 7: Enumerating Large XML Files (Doug Rothaus)"},"content":{"rendered":"<h1 style=\"margin: 24pt 0in 0pt\"><span style=\"font-family: Cambria;color: #365f91\">VB XML Cookbook, Recipe 7: Enumerating Large XML Files (Doug Rothaus)<\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Calibri;font-size: small\">It&rsquo;s been a while since I wrote one of these XML cookbook entries. Here&rsquo;s some info on a common problem: Really big XML files.<\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Calibri;font-size: small\">I&rsquo;m going to show you two things in this recipe. The first is a tip on reading very large XML files while still being able to use XML Axis Properties. The second is how to do make it available to LINQ queries by exposing it as <b>IEnumerable<\/b>.<\/span><\/p>\n<h2 style=\"margin: 10pt 0in 0pt\"><span style=\"font-family: Cambria;color: #4f81bd;font-size: medium\">Reading a Large XML File<\/span><\/h2>\n<p class=\"MsoNormal\"><span style=\"font-family: Calibri;font-size: small\">If you&rsquo;re new to working with XML, there&rsquo;s something important that you need to know. That is, when you load an XML file into an in-memory document, the entire file gets loaded into memory. When working with small XML files, which is often the case, this is no big deal. In fact, it&rsquo;s rather convenient. However, if you are working with an extremely large XML file, this is a big problem. I recently wrote some code to read through a bunch of XML files, not realizing that one of them was over half a gigabyte! My code loaded the entire file into memory using the <b>XDocument.Load()<\/b> method&mdash;well, it tried to at least. Needless to say, when I hit the huge file, my app did not perform well.<\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Calibri;font-size: small\">How do you read an enormous XML file then? You use the <\/span><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.xml.xmlreader.aspx\"><span style=\"font-family: Calibri;font-size: small\">XmlReader<\/span><\/a><span style=\"font-family: Calibri;font-size: small\"> class, which has been around since the first release of the .NET Framework. It reads through an XML file, but simply places a pointer on the current XML element or attribute as you go through the file. As you read through the file with the <b>XmlReader<\/b> object, you can examine the current XML, decide if you are interested in it, process it, discard it, and move on to the next part of the file. The important thing is that you can minimize how much memory is utilized at any one time in your app. <\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Calibri;font-size: small\">Take heed: you still need to be aware of how you are reading through the file. If you open an <b>XmlReader<\/b>, read to the root element, then load that entire element into memory you haven&rsquo;t solved anything.<\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Calibri;font-size: small\">Now you may be saying that if you use an <b>XmlReader<\/b> object, you don&rsquo;t get all of the cool functionality of XML Axis Properties. That&rsquo;s true, and that&rsquo;s why there&rsquo;s a <b>ReadFrom<\/b> method that reads the XML from your <b>XmlReader<\/b> into an <b>XNode<\/b>, which you can then cast as an <b>XElement<\/b> object and make use of all of the VB XML juicy goodness. Using an <b>XmlReader<\/b> and the <b>ReadFrom<\/b> method together ensures that you only use as much memory as the largest XML element that you load.<\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-size: small\"><span style=\"font-family: Calibri\"><span>&nbsp;<\/span>Let&rsquo;s look at an example. The app that I was working on was reading through XML files that contained reflection information from .NET assemblies. For each member of a particular class, there was an &lt;api&gt; element. Within that &lt;api&gt; element there was a bunch of information about that member, and my app needed to grab some of the info for use in summary counts. Here&rsquo;s an abbreviated XML sample of what the data looked like.<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'Courier New';font-size: 10pt\">&lt;reflection&gt;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'Courier New';font-size: 10pt\"><span>&nbsp; <\/span>&lt;apis&gt;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'Courier New';font-size: 10pt\"><span>&nbsp;&nbsp;&nbsp; <\/span>&lt;api id=&#8221;M:Microsoft.VisualBasic.Strings.Mid(System.String,System.Int32)&#8221;&gt;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'Courier New';font-size: 10pt\"><span>&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;<\/span><span>&nbsp;<\/span>&lt;apidata name=&#8221;Mid&#8221; group=&#8221;member&#8221; subgroup=&#8221;method&#8221; \/&gt;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'Courier New';font-size: 10pt\"><span>&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;<\/span><span>&nbsp;<\/span>&lt;containers&gt;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'Courier New';font-size: 10pt\"><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;<\/span><span>&nbsp;<\/span>&lt;namespace api=&#8221;N:Microsoft.VisualBasic&#8221; \/&gt;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'Courier New';font-size: 10pt\"><span>&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;<\/span><span>&nbsp;&nbsp;<\/span>&lt;type api=&#8221;T:Microsoft.VisualBasic.Strings&#8221; ref=&#8221;true&#8221; \/&gt;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'Courier New';font-size: 10pt\"><span>&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;<\/span><span>&nbsp;<\/span>&lt;\/containers&gt;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'Courier New';font-size: 10pt\"><span>&nbsp;<\/span><span>&nbsp; <\/span><span>&nbsp;<\/span>&lt;\/api&gt;<span>&nbsp; <\/span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'Courier New';font-size: 10pt\"><span>&nbsp;&nbsp;&nbsp; <\/span>&lt;api id=&#8221;M:Microsoft.VisualBasic.Strings.Mid(System.String,System.Int32,System.Int32)&#8221;&gt;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'Courier New';font-size: 10pt\"><span>&nbsp; <\/span><span>&nbsp;&nbsp;&nbsp;&nbsp;<\/span>&lt;apidata name=&#8221;Mid&#8221; group=&#8221;member&#8221; subgroup=&#8221;method&#8221; \/&gt;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'Courier New';font-size: 10pt\"><span>&nbsp;<\/span><span>&nbsp; <\/span><span>&nbsp;&nbsp;&nbsp;<\/span>&lt;containers&gt;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'Courier New';font-size: 10pt\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;<\/span>&lt;namespace api=&#8221;N:Microsoft.VisualBasic&#8221; \/&gt;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'Courier New';font-size: 10pt\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;<\/span>&lt;type api=&#8221;T:Microsoft.VisualBasic.Strings&#8221; ref=&#8221;true&#8221; \/&gt;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'Courier New';font-size: 10pt\"><span>&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;<\/span>&lt;\/containers&gt;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'Courier New';font-size: 10pt\"><span>&nbsp; <\/span><span>&nbsp;&nbsp;<\/span>&lt;\/api&gt;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'Courier New';font-size: 10pt\"><span>&nbsp; <\/span>&lt;\/apis&gt;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'Courier New';font-size: 10pt\">&lt;\/reflection&gt;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'Courier New';font-size: 10pt\"><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Calibri;font-size: small\">Here&rsquo;s <span>&nbsp;<\/span>some code to read through each &lt;api&gt; element, one at a time, with an <b>XmlReader<\/b> object. Once I have loaded the &lt;api&gt; element into an <b>XElement<\/b> object, I can use XML Axis properties to get values from the XML contained in the element. The most memory that I use is determined by the largest &lt;api&gt; element rather than the entire file.<\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'Courier New';font-size: 10pt\"><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p><span style=\"font-family: Consolas;font-size: 9.5pt\"><span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">Dim<\/span> reader = Xml.<span style=\"color: #2b91af\">XmlReader<\/span>.Create(<span style=\"color: #a31515\">&#8220;&#8230;.reflectionData.xml&#8221;<\/span>)<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>reader.MoveToContent()<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">While<\/span> reader.ReadToFollowing(<span style=\"color: #a31515\">&#8220;api&#8221;<\/span>)<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">Dim<\/span> api = <span style=\"color: blue\">TryCast<\/span>(<span style=\"color: #2b91af\">XElement<\/span>.ReadFrom(reader), <span style=\"color: #2b91af\">XElement<\/span>)<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">If<\/span> api <span style=\"color: blue\">Is<\/span> <span style=\"color: blue\">Nothing<\/span> <span style=\"color: blue\">Then<\/span> <span style=\"color: blue\">Continue While<\/span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: green\">&#8216; Get information from the &lt;api&gt; element.<\/span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">Dim<\/span> ns = api<span style=\"color: #6464b9\">.&lt;<\/span>containers<span style=\"color: #6464b9\">&gt;.&lt;<\/span>namespace<span style=\"color: #6464b9\">&gt;.@<\/span>api<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">Dim<\/span> containingType = api<span style=\"color: #6464b9\">.&lt;<\/span>containers<span style=\"color: #6464b9\">&gt;.&lt;<\/span>type<span style=\"color: #6464b9\">&gt;.@<\/span>api<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">End<\/span> <span style=\"color: blue\">While<\/span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>reader.Close()<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p><\/span><\/span><\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p class=\"MsoNormal\">\n<p><span style=\"font-family: Calibri;font-size: small\">&nbsp;<\/span><\/p>\n<\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Calibri;font-size: small\">Now this code simply reads to the first &lt;api&gt; element and then reads all of its sibling &lt;api&gt; elements. If one of the &lt;api&gt; elements has a child &lt;api&gt; element, that child element gets loaded in the call to the <b>ReadFrom<\/b> method and the <b>XmlReader<\/b> object&rsquo;s pointer moves past it. This works fine for my app because none of the &lt;api&gt; elements have child &lt;api&gt; elements. You may have different requirements and need to adjust your code. <\/span><\/p>\n<p class=\"MsoNormal\">\n<p><span style=\"font-family: Calibri;font-size: small\">&nbsp;<\/span><\/p>\n<\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Calibri;font-size: small\">I ran this code on a 5MB file with a little less than 30,000 &lt;api&gt; elements. Loading the entire file into memory consumed over 120MB. Using the <b>XmlReader<\/b>, the code consumed less than 1MB. I gathered memory stats using the <\/span><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.gc.gettotalmemory.aspx\"><span style=\"font-family: Calibri;font-size: small\">GetTotalMemory<\/span><\/a><span style=\"font-family: Calibri;font-size: small\"> method.<\/span><\/p>\n<p class=\"MsoNormal\">\n<p><span style=\"font-family: Calibri;font-size: small\">&nbsp;<\/span><\/p>\n<\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Calibri;font-size: small\">One last thing to note in this section is that you can also run into memory issues when writing to a file. If you create a large <b>XDocument<\/b> in memory, and then write it to a file, you end up consuming the memory required to create the document, which is likely unnecessary. You have a couple of choices to minimize your memory footprint while writing an XML file. Similar to using the <b>XmlReader<\/b> class and the <b>ReadFrom<\/b> method, you can use the <\/span><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.xml.xmlwriter.aspx\"><span style=\"font-family: Calibri;color: #0000ff;font-size: small\">XmlWriter<\/span><\/a><span style=\"font-family: Calibri;font-size: small\"> class and the <b>WriteTo<\/b> method. As another option, you can use the <\/span><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.xml.linq.xstreamingelement.aspx\"><span style=\"font-family: Calibri;font-size: small\">XStreamingElement<\/span><\/a><span style=\"font-family: Calibri;font-size: small\"> class to write a single element at a time from an enumerable source, such as a LINQ query.&nbsp;<\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Calibri;font-size: small\"><\/span><\/p>\n<p><span style=\"font-family: Calibri;font-size: small\">&nbsp;<\/span><\/p>\n<\/p>\n<h2 style=\"margin: 10pt 0in 0pt\"><span style=\"font-family: Cambria;color: #4f81bd;font-size: medium\">What about LINQ Queries?<\/span><\/h2>\n<p class=\"MsoNormal\"><span style=\"font-family: Calibri;font-size: small\">In addition to having a small memory footprint, I also wanted to be able to use LINQ to query a large XML file. This can be achieved by creating a class that implements the <b>IEnumerable<\/b> interface. By fitting the code that I would have used to loop through the XML file into a class that implements <b>IEnumerable(Of XElement)<\/b>, I can use an instance of that class as the source of any number of LINQ queries.<\/span><\/p>\n<p class=\"MsoNormal\">\n<p><span style=\"font-family: Calibri;font-size: small\">&nbsp;<\/span><\/p>\n<\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Calibri;font-size: small\">What I&rsquo;ve created for this step is almost exactly the same as the class created by this walkthrough: <\/span><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd145423.aspx\"><span style=\"font-family: Calibri;font-size: small\">Walkthrough: Implementing IEnumerable(Of T) in Visual Basic<\/span><\/a><span style=\"font-family: Calibri;font-size: small\"> . The walkthrough shows you how to implement <b>IEnumerable(Of String)<\/b> to expose the contents of a text file one line at a time. We&rsquo;ll do the same with an XML file.<\/span><\/p>\n<p class=\"MsoNormal\">\n<p><span style=\"font-family: Calibri;font-size: small\">&nbsp;<\/span><\/p>\n<\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Calibri;font-size: small\">When you implement <b>IEnumerable<\/b>, you actually need to implement both <b>IEnumerable<\/b> and <b>IEnumerator<\/b>. The bulk of your code goes into the <b>IEnumerator<\/b> implementation. You could create one class that implements both, but I like to split them into two classes.<\/span><\/p>\n<p class=\"MsoNormal\">\n<p><span style=\"font-family: Calibri;font-size: small\">&nbsp;<\/span><\/p>\n<\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Calibri;font-size: small\">I&rsquo;ve called the class that implements <b>IEnumerable(Of<span>&nbsp; <\/span>XElement)<\/b> <\/span><span style=\"font-family: Consolas;color: #2b91af;font-size: 9.5pt\">XmlReaderEnumerable<\/span><span style=\"font-size: small\"><span style=\"font-family: Calibri\">. Following that naming convention I&rsquo;ve called the class that implements <b>IEnumerator(Of XElement) <\/b><\/span><\/span><span style=\"font-family: Consolas;color: #2b91af;font-size: 9.5pt\">XmlReaderEnumerator<\/span><span style=\"font-family: Calibri;font-size: small\">. The behavior is the same as the earlier <b>XmlReader<\/b> example. The <\/span><span style=\"font-family: Consolas;color: #2b91af;font-size: 9.5pt\">XmlReaderEnumerator<\/span><span style=\"font-family: Calibri;font-size: small\"> class finds the first instance of a particular element, and then finds all of its sibling elements of the same name. As a result, I&rsquo;ve added a constructor that takes both the path to the XML file, and the name of the XML element to search for. Note that the name is case sensitive as XML is case sensitive. <\/span><\/p>\n<p class=\"MsoNormal\">\n<p><span style=\"font-family: Calibri;font-size: small\">&nbsp;<\/span><\/p>\n<\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Calibri;font-size: small\">The <\/span><span style=\"font-family: Consolas;color: #2b91af;font-size: 9.5pt\">XmlReaderEnumerable<\/span><span style=\"font-family: Calibri;font-size: small\"> class doesn&rsquo;t do much. All it does is return a reference to an instance of the <\/span><span style=\"font-family: Consolas;color: #2b91af;font-size: 9.5pt\">XmlReaderEnumerator<\/span><span style=\"font-family: Calibri;font-size: small\"> class. Here&rsquo;s the code.<\/span><\/p>\n<p class=\"MsoNormal\">\n<p><span style=\"font-family: Calibri;font-size: small\">&nbsp;<\/span><\/p>\n<\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;color: blue;font-size: 9.5pt\">Public<\/span><span style=\"font-family: Consolas;font-size: 9.5pt\"> <span style=\"color: blue\">Class<\/span> <span style=\"color: #2b91af\">XmlReaderEnumerable<\/span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">Implements<\/span> <span style=\"color: #2b91af\">IEnumerable<\/span>(<span style=\"color: blue\">Of<\/span> <span style=\"color: #2b91af\">XElement<\/span>)<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">Private<\/span> _filePath <span>As<\/span> <span style=\"color: blue\">String<\/span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">Private<\/span> _elementName <span style=\"color: blue\">As<\/span> <span style=\"color: blue\">String<\/span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">Public<\/span> <span style=\"color: blue\">Sub<\/span> <span style=\"color: blue\">New<\/span>(<span style=\"color: blue\">ByVal<\/span> filePath <span style=\"color: blue\">As<\/span> <span style=\"color: blue\">String<\/span>, <span style=\"color: blue\">ByVal<\/span> elementName <span style=\"color: blue\">As<\/span> <span style=\"color: blue\">String<\/span>)<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>_filePath = filePath<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>_elementName = elementName<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">End<\/span> <span style=\"color: blue\">Sub<\/span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">Public<\/span> <span style=\"color: blue\">Function<\/span> GetEnumerator() <span style=\"color: blue\">As<\/span> <span style=\"color: #2b91af\">IEnumerator<\/span>(<span style=\"color: blue\">Of<\/span> <span style=\"color: #2b91af\">XElement<\/span>) _<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">Implements<\/span> <span style=\"color: #2b91af\">IEnumerable<\/span>(<span style=\"color: blue\">Of<\/span> <span style=\"color: #2b91af\">XElement<\/span>).GetEnumerator<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">Return<\/span> <span style=\"color: blue\">New<\/span> <span style=\"color: #2b91af\">XmlReaderEnumerator<\/span>(_filePath, _elementName)<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">End<\/span> <span style=\"color: blue\">Function<\/span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">Private<\/span> <span style=\"color: blue\">Function<\/span> GetEnumerator1() <span style=\"color: blue\">As<\/span> <span style=\"color: #2b91af\">IEnumerator<\/span> _<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">Implements<\/span> IEnumerable.GetEnumerator<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">Return<\/span> <span style=\"color: blue\">Me<\/span>.GetEnumerator()<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">End<\/span> <span style=\"color: blue\">Function<\/span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;color: blue;font-size: 9.5pt\">End<\/span><span style=\"font-family: Consolas;font-size: 9.5pt\"> <span style=\"color: blue\">Class<\/p>\n<p><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;color: #2b91af;font-size: 9.5pt\"><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Calibri;font-size: small\">The <\/span><span style=\"font-family: Consolas;color: #2b91af;font-size: 9.5pt\">XmlReaderEnumerator<\/span><span style=\"font-family: Calibri;font-size: small\"> class is where the code resides to read through the XML file. In the constructor, it opens the file and moves to the start of the XML content. In the <b>MoveNext<\/b> method, it reads to the element of the supplied name (for example, &ldquo;api&rdquo;). In the <b>Dispose<\/b> method, it closes the reader. That&rsquo;s it. It looks like a lot of code, but it really isn&rsquo;t.<\/span><\/p>\n<p class=\"MsoNormal\">\n<p><span style=\"font-family: Calibri;font-size: small\">&nbsp;<\/span><\/p>\n<\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;color: blue;font-size: 9.5pt\">Public<\/span><span style=\"font-family: Consolas;font-size: 9.5pt\"> <span style=\"color: blue\">Class<\/span> <span style=\"color: #2b91af\">XmlReaderEnumerator<\/span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">Implements<\/span> <span style=\"color: #2b91af\">IEnumerator<\/span>(<span style=\"color: blue\">Of<\/span> <span style=\"color: #2b91af\">XElement<\/span>)<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">Private<\/span> _xmlReader <span style=\"color: blue\">As<\/span> Xml.<span style=\"color: #2b91af\">XmlReader<\/span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">Private<\/span> _elementName <span style=\"color: blue\">As<\/span> <span style=\"color: blue\">String<\/span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">Private<\/span> _filePath <span style=\"color: blue\">As<\/span> <span style=\"color: blue\">String<\/span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">Public<\/span> <span style=\"color: blue\">Sub<\/span> <span style=\"color: blue\">New<\/span>(<span style=\"color: blue\">ByVal<\/span> filePath <span style=\"color: blue\">As<\/span> <span style=\"color: blue\">String<\/span>, <span style=\"color: blue\">ByVal<\/span> elementName <span style=\"color: blue\">As<\/span> <span style=\"color: blue\">String<\/span>)<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>_filePath = filePath<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>_elementName = elementName<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>_xmlReader = Xml.<span style=\"color: #2b91af\">XmlReader<\/span>.Create(_filePath)<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>_xmlReader.MoveToContent()<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">End<\/span> <span style=\"color: blue\">Sub<\/span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">Private<\/span> _current <span style=\"color: blue\">As<\/span> <span style=\"color: #2b91af\">XElement<\/span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: Consolas;font-size: 9.5pt\"><span>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">Public<\/span> <span style=\"color: blue\">ReadOnly<\/span> <span style=\"color: blue\">Property<\/span> Current() <span style=\"color: blue\">As<\/span> <span style=\"color: #2b91af\">XElement<\/span><\/span><\/p>\n<\/h2>\n","protected":false},"excerpt":{"rendered":"<p>VB XML Cookbook, Recipe 7: Enumerating Large XML Files (Doug Rothaus) It&rsquo;s been a while since I wrote one of these XML cookbook entries. Here&rsquo;s some info on a common problem: Really big XML files. I&rsquo;m going to show you two things in this recipe. The first is a tip on reading very large XML [&hellip;]<\/p>\n","protected":false},"author":260,"featured_media":8818,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[195],"tags":[59,163,166,167,185],"class_list":["post-843","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-visual-basic","tag-doug-rothaus","tag-vb-xml-cookbook","tag-vb2008","tag-vb2010","tag-xml"],"acf":[],"blog_post_summary":"<p>VB XML Cookbook, Recipe 7: Enumerating Large XML Files (Doug Rothaus) It&rsquo;s been a while since I wrote one of these XML cookbook entries. Here&rsquo;s some info on a common problem: Really big XML files. I&rsquo;m going to show you two things in this recipe. The first is a tip on reading very large XML [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/843","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/users\/260"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/comments?post=843"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/843\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media\/8818"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media?parent=843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/categories?post=843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/tags?post=843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}