using System; using System.Collections.Generic; using System.IO; using System.Xml.Linq; namespace TapeCatalog.Methods { /// /// Helpers to read XML files and produce a compact summary used by the UI. /// public static class Xml { public static List ParseSummary(string filePath) { var result = new List(); if (string.IsNullOrWhiteSpace(filePath) || !File.Exists(filePath)) return result; try { var doc = XDocument.Load(filePath); var root = doc.Root; if (root == null) return result; if (string.Equals(root.Name.LocalName, "Files", StringComparison.OrdinalIgnoreCase)) { foreach (var fe in root.Elements("File")) { try { var fn = fe.Element("Filename")?.Value ?? string.Empty; var fd = fe.Element("FileDir")?.Value ?? string.Empty; var st = fe.Element("Status")?.Value ?? string.Empty; result.Add(new TapeCatalog.FileEntry { Filename = fn, FileDir = fd, Status = st }); } catch { // ignore malformed entries } } } else if (string.Equals(root.Name.LocalName, "TapeEntries", StringComparison.OrdinalIgnoreCase)) { foreach (var entry in root.Elements("Entry")) { try { var fn = entry.Element("Filename")?.Value ?? string.Empty; var med = entry.Element("Medium")?.Value ?? string.Empty; var mod = entry.Element("Modified")?.Value ?? string.Empty; result.Add(new TapeCatalog.FileEntry { Filename = fn, FileDir = med, Status = mod }); } catch { // ignore malformed entries } } } } catch { // ignore parse errors and return empty list } return result; } public static string[] GetXmlFiles(string xmlDirectory) { if (string.IsNullOrWhiteSpace(xmlDirectory) || !Directory.Exists(xmlDirectory)) return Array.Empty(); try { return Directory.GetFiles(xmlDirectory, "*.xml", SearchOption.TopDirectoryOnly); } catch { return Array.Empty(); } } } }