XML is pretty old tech and without a schema is a bit of a pain to work with! A semi saving grace is using Visual Studio's "Paste XML as Classes" option (Paste Special) which will generate C# classes capable of representing the XML you had on the clipboard (using the XmlSerializer). However the caveat to this is that it only generates code for the exact xml you have used, so any optional attributes/elements or collections that only have 1 item in them will be generated incorrectly and will silently start dropping information when you deserialize another file with slightly different xml content. To combat this, I wrote a simple XmlSchemaChecker class which takes the content of an XML file and it's deserialized equivalent and ensures that every piece of data from the file is represented within the instance. It logs these problems when running with Debug logging enabled and is called from the class responsible for deserializing files.

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Serialization;
using Microsoft.Extensions.Logging;

namespace Deserialization
{
    public class XmlSchemaChecker : IXmlSchemaChecker
    {
        private readonly ILogger<XmlSchemaChecker> _logger;

        public XmlSchemaChecker(ILogger<XmlSchemaChecker> logger)
        {
            _logger = logger ?? throw new ArgumentNullException(nameof(logger));
        }

        public void LogSchemaWarnings<T>(string originalXmlFilePath, T deserialized)
        {
            if (!_logger.IsEnabled(LogLevel.Debug)) return;

            var originalXml = File.ReadAllText(originalXmlFilePath);
            var newXml = ReSerialize(deserialized);

            var originalValues = GetXmlValues(originalXml);
            var newValues = GetXmlValues(newXml);

            var missingItems = originalValues.Except(newValues).ToList();

            if (missingItems.Any())
            {
                _logger.LogDebug("Schema for {filename} was not fully deserialized. Missing items: {missingItems}", originalXmlFilePath, missingItems);
            }
        }

        private static void ProcessNodes(ISet<string> values, Stack<string> paths, IEnumerable nodes)
        {
            foreach (var node in nodes)
            {
                switch (node)
                {
                    case XmlDeclaration _:
                        continue;
                    case XmlElement element:
                        {
                            paths.Push(element.Name);

                            foreach (var att in element.Attributes)
                            {
                                if (att is XmlAttribute xmlAttribute && xmlAttribute.Name != "xmlns:xsd" && xmlAttribute.Name != "xmlns:xsi")
                                {
                                    values.Add($"{string.Join(":", paths.Reverse())}:{xmlAttribute.Name}:{CleanseValue(xmlAttribute.Value)}");
                                }
                            }

                            if (element.HasChildNodes)
                            {
                                ProcessNodes(values, paths, element.ChildNodes);
                            }

                            paths.Pop();
                            break;
                        }
                    case XmlText text:
                        {
                            values.Add($"{string.Join(":", paths.Reverse())}:{text.ParentNode.Name}:{CleanseValue(text.InnerText)}");
                            break;
                        }
                }
            }
        }

        private static string CleanseValue(string value)
        {
            return value.Replace("\r\n", "\n").Replace("\t", "").Trim(' ', '\n');
        }

        private static IEnumerable<string> GetXmlValues(string xml)
        {
            var values = new HashSet<string>();
            var paths = new Stack<string>();
            var doc = new XmlDocument();
            doc.LoadXml(xml);

            ProcessNodes(values, paths, doc.ChildNodes);

            return values;
        }

        private static string ReSerialize<T>(T item)
        {
            var xmlSerializer = new XmlSerializer(typeof(T));
            var output = new System.Text.StringBuilder();

            using (var outputStream = new StringWriter(output))
            {
                xmlSerializer.Serialize(outputStream, item);
            }

            return output.ToString();
        }
    }
}