Home
VB.NET
XmlReader, Parse XML File
This page was last reviewed on Sep 28, 2022.
Dot Net Perls
XmlReader. This class reads XML data. With this System.Xml type, we sequentially read in each element of an XML file. This reduces memory usage and improves performance.
File
XmlWriter
Some notes. The XmlReader requires we call the Read() function in a loop. This may not be as simple as other approaches, but it often is faster.
XElement
This example uses a simple XML file. It contains a perls element and also several article elements. The article element has an attribute "name" as well.
Here We call XmlReader.Create to open an XmlReader on the target file. Next, we loop through the entire file using While reader.Read().
Detail We detect start elements using IsStartElement. Then, we can access the Name property on the XmlReader to get the current tag name.
Detail To get an attribute, use the indexer reader("name") where "name" is the attribute identifier.
Tip You can specialize logic based on what the current element is—just use If-statements or Select Case statements.
If
Select Case
Imports System.Xml Module Module1 Sub Main() ' Create an XML reader. Using reader As XmlReader = XmlReader.Create("C:\perls.xml") While reader.Read() ' Check for start elements. If reader.IsStartElement() Then ' See if perls element or article element. If reader.Name = "perls" Then Console.WriteLine("Start <perls> element.") ElseIf reader.Name = "article" Then Console.WriteLine("Start <article> element.") ' Get name attribute. Dim attribute As String = reader("name") If attribute IsNot Nothing Then Console.WriteLine(" Has attribute name: {0}", attribute) End If ' Text data. If reader.Read() Then Console.WriteLine(" Text node: {0}", reader.Value.Trim()) End If End If End If End While End Using End Sub End Module
<?xml version="1.0" encoding="utf-8" ?> <perls> <article name="backgroundworker"> Example text. </article> <article name="threadpool"> More text. </article> <article></article> <article>Final text.</article> </perls>
Start <perls> element. Start <article> element. Has attribute name: backgroundworker Text node: Example text. Start <article> element. Has attribute name: threadpool Text node: More text. Start <article> element. Text node: Start <article> element. Text node: Final text.
Value. Text nodes can be accessed with the reader.Value property. This returns the text node if the XmlReader is currently on a text node. The returned value is a String.
Summary. An XmlReader provides a fast way to read in XML files. You have to loop over each element with the Read function, and then test the properties of the XmlReader.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 28, 2022 (edit).
Home
Changes
© 2007-2024 Sam Allen.