using System.Collections.Generic;
public static List<string> GetTitlesPublishedAfter2000(string xmlFilePath)
if (string.IsNullOrEmpty(xmlFilePath))
throw new ArgumentException("The XML file path cannot be null or empty.", nameof(xmlFilePath));
if (!System.IO.File.Exists(xmlFilePath))
throw new System.IO.FileNotFoundException("The XML file does not exist.", xmlFilePath);
List<string> titles = new List<string>();
XmlDocument doc = new XmlDocument();
XmlNodeList bookNodes = doc.SelectNodes("/catalog/book");
foreach (XmlNode bookNode in bookNodes)
XmlNode titleNode = bookNode.SelectSingleNode("title");
XmlNode yearNode = bookNode.SelectSingleNode("year");
if (titleNode != null && yearNode != null)
string title = titleNode.InnerText;
if (int.TryParse(yearNode.InnerText, out int year) && year > 2000)
throw new XmlException("The XML file is invalid.", ex);
Console.WriteLine($"An error occurred: {ex.Message}");
return new List<string>();
public static void Main(string[] args)
string xmlFilePath = "books.xml";
string xmlContent = @"<?xml version=""1.0"" encoding=""utf-8""?>
<title>The Lord of the Rings</title>
<author>J.R.R. Tolkien</author>
<title>Harry Potter and the Philosopher's Stone</title>
<author>J.K. Rowling</author>
<title>The Hitchhiker's Guide to the Galaxy</title>
<author>Douglas Adams</author>
<title>The Name of the Wind</title>
<author>Patrick Rothfuss</author>
<title>A book after 2000</title>
<author>An Author</author>
System.IO.File.WriteAllText(xmlFilePath, xmlContent);
List<string> titles = GetTitlesPublishedAfter2000(xmlFilePath);
Console.WriteLine("Books published after 2000:");
foreach (string title in titles)
Console.WriteLine($"- {title}");
Console.WriteLine("No books published after 2000 found.");
catch (ArgumentException ex)
Console.WriteLine($"Error: {ex.Message}");
catch (System.IO.FileNotFoundException ex)
Console.WriteLine($"Error: {ex.Message}");
Console.WriteLine($"Error: {ex.Message}");
Console.WriteLine($"An unexpected error occurred: {ex.Message}");