SHA256
84 lines
2.9 KiB
C#
84 lines
2.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Xml.Linq;
|
|
|
|
namespace TapeCatalog.Methods
|
|
{
|
|
/// <summary>
|
|
/// Helpers to read XML files and produce a compact summary used by the UI.
|
|
/// </summary>
|
|
public static class Xml
|
|
{
|
|
public static List<TapeCatalog.FileEntry> ParseSummary(string filePath)
|
|
{
|
|
var result = new List<TapeCatalog.FileEntry>();
|
|
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<string>();
|
|
|
|
try
|
|
{
|
|
return Directory.GetFiles(xmlDirectory, "*.xml", SearchOption.TopDirectoryOnly);
|
|
}
|
|
catch
|
|
{
|
|
return Array.Empty<string>();
|
|
}
|
|
}
|
|
}
|
|
}
|