How to read XML in C#
By FoxLearn 1/17/2025 4:11:50 AM 83
Now, we’ll focus on reading and extracting data from an existing XML file using the XmlReader
class.
How to read XML in C#
Here's an example XML file we'll work with:
<?xml version="1.0" encoding="utf-8"?> <Library> <FilePath>D:\Books\LibraryData</FilePath> <Books> <Book> <Title>Introduction to C#</Title> <Author>John Doe</Author> </Book> <Book> <Title>Mastering LINQ</Title> <Author>Jane Smith</Author> </Book> </Books> </Library>
XML Structure
- Parent Node: The root node
<Library>
serves as the parent. - Child Nodes:
<FilePath>
and<Book>
are child nodes. Each<Book>
node contains additional child nodes,<Title>
and<Author>
.
Using XmlReader in C#
The XmlReader
class provides a fast, forward-only, read-only mechanism for accessing XML data. It's ideal for large XML files due to its low memory consumption.
Below is the code snippet to parse the XML and extract details such as file path and book information:
using System; using System.Collections.Generic; using System.Xml; class Program { static void Main(string[] args) { string path = @"D:\YourFolder"; string xmlFilePath = System.IO.Path.Combine(path, "library.xml"); string filePath = string.Empty; List<(string Title, string Author)> books = new List<(string, string)>(); using (XmlReader reader = XmlReader.Create(xmlFilePath)) { string currentElement = string.Empty; while (reader.Read()) { if (reader.IsStartElement()) { switch (reader.Name) { case "FilePath": filePath = reader.ReadString(); break; case "Title": currentElement = "Title"; break; case "Author": currentElement = "Author"; break; case "Book": string title = null, author = null; while (reader.Read() && reader.Name != "Book") { if (reader.IsStartElement() && reader.Name == "Title") title = reader.ReadString(); if (reader.IsStartElement() && reader.Name == "Author") author = reader.ReadString(); } books.Add((title, author)); break; } } } } Console.WriteLine($"Library Path: {filePath}"); Console.WriteLine("Books List:"); foreach (var book in books) { Console.WriteLine($"- {book.Title} by {book.Author}"); } } }
In this example:
- XmlReader Creation: The
XmlReader
is initialized using theCreate()
method. - File Parsing: Inside a
using
block, the XML file is read node by node usingreader.Read()
. - Conditional Logic: Based on the
reader.Name
, we extract specific data. For nested nodes, such as<Book>
, a loop ensures that all child elements are processed. - Output: Extracted data is displayed in the console.
Using XmlDocument in C#
The XmlDocument
class represents an in-memory representation of an XML document and allows DOM (Document Object Model)-style navigation and manipulation.
using System; using System.Xml; class Program { static void Main(string[] args) { string xmlFilePath = @"C:\Path\To\Your\File.xml"; XmlDocument doc = new XmlDocument(); doc.Load(xmlFilePath); XmlNode root = doc.DocumentElement; foreach (XmlNode node in root.SelectNodes("Books/Book")) { Console.WriteLine($"Title: {node["Title"]?.InnerText}"); Console.WriteLine($"Author: {node["Author"]?.InnerText}"); } } }
Using LINQ to XML in C#
The System.Xml.Linq
namespace allows querying XML data using LINQ, which is concise and easy to understand.
using System; using System.Linq; using System.Xml.Linq; class Program { static void Main(string[] args) { string xmlFilePath = @"C:\Path\To\Your\File.xml"; XDocument doc = XDocument.Load(xmlFilePath); var books = from book in doc.Descendants("Book") select new { Title = book.Element("Title")?.Value, Author = book.Element("Author")?.Value }; foreach (var book in books) { Console.WriteLine($"Title: {book.Title}"); Console.WriteLine($"Author: {book.Author}"); } } }
Using DataSet in C#
The DataSet
class can read XML and treat it as structured data, which is useful for tabular data.
using System; using System.Data; class Program { static void Main(string[] args) { string xmlFilePath = @"C:\Path\To\Your\File.xml"; DataSet ds = new DataSet(); ds.ReadXml(xmlFilePath); foreach (DataRow row in ds.Tables["Book"].Rows) { Console.WriteLine($"Title: {row["Title"]}"); Console.WriteLine($"Author: {row["Author"]}"); } } }
Each approach has its strengths, and the choice depends on your specific requirements and the structure of your XML data.
- How to fix 'Failure sending mail' in C#
- How to Parse a Comma-Separated String from App.config in C#
- How to convert a dictionary to a list in C#
- How to retrieve the Executable Path in C#
- How to validate an IP address in C#
- How to retrieve the Downloads Directory Path in C#
- C# Tutorial
- Dictionary with multiple values per key in C#