TheDeveloperBlog.com

Home | Contact Us

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

<< Back to C-SHARP

C# XmlTextReader

XmlTextReader. With XmlTextReader we parse XML data. This type acts upon a string containing XML markup. We use the XmlTextReader constructor and develop a custom parser for XML data. This is an efficient approach to XML parsing.XmlTextWriter
Example. Let's get started with this simple example program. A complete XML document is stored inside a string literal. With the StringReader constructor, we read this string as a StringReader.String Literal

Next: We pass the StringReader to the XmlTextReader constructor. In the while-loop, we read nodes and proceed if we detect a start element.

StringReader

Switch: We process each tag name differently in the switch statement. We use ReadString to read in the next node as a string.

String Switch
C# program that uses XmlTextReader using System; using System.IO; using System.Xml; class Program { static void Main() { string input = @"<?xml version=""1.0"" encoding=""utf-16""?><List> <Employee><ID>1</ID><First>David</First> <Last>Smith</Last><Salary>10000</Salary></Employee> <Employee><ID>3</ID><First>Mark</First> <Last>Drinkwater</Last><Salary>30000</Salary></Employee> <Employee><ID>4</ID><First>Norah</First> <Last>Miller</Last><Salary>20000</Salary></Employee> <Employee><ID>12</ID><First>Cecil</First> <Last>Walker</Last><Salary>120000</Salary></Employee> </List>"; using (StringReader stringReader = new StringReader(input)) using (XmlTextReader reader = new XmlTextReader(stringReader)) { while (reader.Read()) { if (reader.IsStartElement()) { switch (reader.Name) { case "Employee": Console.WriteLine(); break; case "ID": Console.WriteLine("ID: " + reader.ReadString()); break; case "First": Console.WriteLine("First: " + reader.ReadString()); break; case "Last": Console.WriteLine("Last: " + reader.ReadString()); break; case "Salary": Console.WriteLine("Salary: " + reader.ReadString()); break; } } } } } } Output ID: 1 First: David Last: Smith Salary: 10000 ID: 3 First: Mark Last: Drinkwater Salary: 30000 ID: 4 First: Norah Last: Miller Salary: 20000 ID: 12 First: Cecil Last: Walker Salary: 120000
XmlReader. XmlReader and XmlTextReader are basically the same. When you call XmlTextReader.Create, an XmlReader is returned. But the XmlTextReader provides more constructors. The functionality is essentially the same.XmlReader
Summary. We looked at the XmlTextReader type in the C# programming language and the .NET Framework. With XmlTextReader, we gain more constructors for using an XmlReader. These types can be used to develop fast custom parsers for XML files.

But: If execution speed is not important, other types such as XElement can lead to faster development.

XElement
© 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