Home
C#
XmlReader, Parse XML File
This page was last reviewed on Feb 24, 2024.
Dot Net Perls
XmlReader. This 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.
XmlWriter
This type provides a forward-only parsing object for the underlying XML files. You must manage the position in the file logically as the parser can only go forward.
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.
Property
Start We instantiate an XmlReader instance by assigning an XmlReader reference to the result of the XmlReader.Create static method.
Next We see a while-loop construct that evaluates the result of the Read instance method in its expression body.
while
Info IsStartElement returns true if the element is not an end tag. It returns true for "article" but false for "/article".
Note The attribute "name" is accessed with the "name" argument in an indexer. This is null when not found.
Indexer
using System; using System.Xml; class Program { static void Main() { // Create an XML reader for this file. using (XmlReader reader = XmlReader.Create("perls.xml")) { while (reader.Read()) { // Only detect start elements. if (reader.IsStartElement()) { // Get element name and switch on it. switch (reader.Name) { case "perls": // Detect this element. Console.WriteLine("Start <perls> 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; } } } } } }
<?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.
A discussion. Unfortunately, the XmlReader will introduce 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.
Also Types such as XElement can do this (with XElement.Load). But they can greatly expand memory usage and reduce performance.
XElement
Thus The XmlReader retains more complexity. With it you can manipulate the XML at a level more suited to many programs.
Summary. We used the XmlReader type. This class can be used to implement higher-level parsing code, while retaining top performance and low memory usage.
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 Feb 24, 2024 (edit).
Home
Changes
© 2007-2024 Sam Allen.