TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET XmlReader, Parse XML File

Use XmlReader on an XML file. XmlReader is a fast way to read XML.
XmlReader reads XML data in an efficient way. With this System.Xml type, we sequentially read in each element of an XML file. This reduces memory usage and improves performance. We provide an example for XmlReader.XmlWriterFile
Example. This example uses a simple XML file located at C:\Codex.xml. It contains a <Codex> 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().

Loop: We detect start elements using IsStartElement. Then, we can access the Name property on the XmlReader to get the current tag name.

Attribute: 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 ThenSelect Case
VB.NET program that uses XmlReader Imports System.Xml Module Module1 Sub Main() ' Create an XML reader. Using reader As XmlReader = XmlReader.Create("C:\Codex.xml") While reader.Read() ' Check for start elements. If reader.IsStartElement() Then ' See if Codex element or article element. If reader.Name = "Codex" Then Console.WriteLine("Start <Codex> 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 Input text: Codex.xml <?xml version="1.0" encoding="utf-8" ?> <Codex> <article name="backgroundworker"> Example text. </article> <article name="threadpool"> More text. </article> <article></article> <article>Final text.</article> </Codex> Output Start <Codex> 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 in the XML file 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 instance.Strings
Summary. This example showed some parts of using the XmlReader type. This type 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.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf