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# XmlReader, Parse XML File

Use the XmlReader class for fast XML file reading. XmlReader scans files for important data.
XmlReader opens and parses XML files. It handles attribute values, text nodes and multiple tag names. It provides a lower-level abstraction over the XML file structure. This is more complex than other solutions but benefits performance.XmlWriter
Conceptually, the XmlReader provides a forward-only parsing object for the underlying XML files. In other words, you must manage the position in the file logically in your code as the parser can only go forward.

Info: XmlReader reduces the memory space required for the parser. It is efficient.

Example. You can use certain methods, such as IsStartElement and the Name property, to detect the location in the file and then execute conditional logic based on this location. This increases complexity.Property

Create: We instantiate an XmlReader instance by assigning an XmlReader reference to the result of the XmlReader.Create static method.

While: We see a while-loop construct that evaluates the result of the Read instance method in its expression body.

While

IsStartElement: This returns true if the element is not an end tag. It returns true for "<article>" but false for "</article>".

Name: The attribute "name" is accessed with the "name" argument in an indexer. This is null when not found.

Indexer
C# program that uses XmlReader type using System; using System.Xml; class Program { static void Main() { // Create an XML reader for this file. using (XmlReader reader = XmlReader.Create("Codex.xml")) { while (reader.Read()) { // Only detect start elements. if (reader.IsStartElement()) { // Get element name and switch on it. switch (reader.Name) { case "Codex": // Detect this element. Console.WriteLine("Start <Codex> element."); break; case "article": // Detect this article element. Console.WriteLine("Start <article> element."); // Search for the attribute name on this current node. string attribute = reader["name"]; if (attribute != null) { Console.WriteLine(" Has attribute name: " + attribute); } // Next read will contain text. if (reader.Read()) { Console.WriteLine(" Text node: " + reader.Value.Trim()); } break; } } } } } } 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.
Benefits. Let's review the benefits of XmlReader as opposed to some solutions such as the XElement type. Unfortunately, the XmlReader will introduce more complexity into the parsing code in your program, due to its nature as a forward-only parser.

And: XmlReader will not parse an entire file into an object model automatically.

Types such as XElement can do this, as with the XElement.Load method. But they can greatly expand memory usage and reduce performance by forcing unnecessary disk IO and allocations.XElementOptimization
Summary. We looked at a simple example of the XmlReader type in the C# language. This class can be used to implement higher-level parsing code, while retaining top performance and low memory usage.

Thus: The XmlReader retains more complexity. With it you can manipulate the XML at a level more suited to many programs.

© 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